Hasan A Yousef
Hasan A Yousef

Reputation: 24988

Optional RegExp identification not working in ES6

As per this the below are JavaScript ES6 RegExp:

The ? matches optional character
The . matches any single character except line terminators
The \w Matches any alphanumeric character
The * Matches the preceding item 0 or more times

I'm trying with this to test the following expressions:

"show .* report of (?<day>\w+)"

The above, matches the: show me the report of Monday But failed, and did not match the show report of Monday

I assume .* means zero/null or more characters, but looks it is not working with no character! I tried to use ? as well but failed.

Any explanation and help pls.

Upvotes: 0

Views: 32

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272246

show .* report matches show, a space, zero or more characters and another space. show report of Monday has single space between the two words. A better solution is this regex:

/show (?:.+ )?report of (?<day>\w+)/

Or this, except that it matches "show misreport of Monday" as well:

/show .*report of (?<day>\w+)/

The regex is explained in this demo.

Upvotes: 3

Related Questions