Skip to content

Incorrect returns of start and end methods of Matcher from the regex library. #253

@warownia1

Description

@warownia1

According to the documentation, the method start(int) (or end(int)) should return the start (end) index of the captured group or -1 if the match was successful, but the group did not match. With capturing group 0 denoting the entire pattern,
the expressions m.start() and m.start(0) (or m.end() and m.end(0)) are equivalent. Java2script implementation of the Matcher returns -1 for the start and the end of group 0 despite a successful match.

Example:

String lipsum = "Lorem ipsum dolor sit amet";
Pattern pattern = Pattern.compile("ipsum");
Matcher m = pattern.matcher(lipsum);
m.find();
int start = m.start();
int end = m.end();
int start0 = m.start(0);
int end0 = m.end(0);
System.out.format("%d %d%n", start, end);
System.out.format("%d %d%n", start0, end0);

The code outputs 6 11 twice in java, but it outputs 6 11 and -1 -1 in javascript.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions