Aadesh Magare
Aadesh Magare

Reputation: 47

Exclude some characters from word boundary in javascript regular expressions

days = /\b\d{2}\b/; date = /\b\d{4}-\d{2}-\d{2}\b/;

2020-12-22 should match date and not days but it matches both. is it possible to make \b
not treat - as word boundary?

Upvotes: 1

Views: 1499

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

There are several questions in your current question.

Is it possible to make \b not treat - as word boundary?

See this tchrist's answer about word boundaries in the Exploring Boundaries section. That is how it works, and there is no way to redefine \b behavior.

2020-12-22 should match date and not days but it matches both.

To match days and avoid matching dates with days regex, you would need lookbehind and lookahead - /\b(?<!-)\d{2}\b(?!-)/ - but JavaScript regex does not support a lookbehind construct. All you can do is use a consuming pattern instead that will match the start of string or any char but a hyphen - (?:^|[^-]), and use a capturing group around \d{2} to capture it into a separate group. Note that depending on what you are doing you might also need to use a capturing group in the lookbehind workaround pattern.

If you plan to extract, use

var days = /(?:^|[^-])\b(\d{2})\b(?!-)/g;
var s = "25 and 45 on 2017-04-14 and 2017-04-15.";
var res = [], m;
while ((m=days.exec(s)) !== null) {
  res.push(m[1]);
}
console.log(res)

To replace them, use

var days = /(^|[^-])\b(\d{2})\b(?!-)/g;
var s = "25 and 45 on 2017-04-14 and 2017-04-15.";
console.log(s.replace(days, "$1[TAG]$2[/TAG]"));

Upvotes: 2

Related Questions