1

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?

Aadesh Magare
  • 47
  • 1
  • 7
  • Can you specify the order of matching? If not, `var days = /(?:^|[^-])\b(\d{2})\b(?!-)/` should work. Also, you may consider `/\b(?:(\d{4}-\d{2}-\d{2}\b)|(\d{2}))\b/` and check which group contains the value. – Wiktor Stribiżew Apr 13 '17 at 19:55
  • Are you extracting the values from longer text? Then `/\b(?:(\d{4}-\d{2}-\d{2}\b)|(\d{2}))\b/g` looks preferable. – Wiktor Stribiżew Apr 13 '17 at 20:01
  • Show some sample text. Don't make people guess ! –  Apr 13 '17 at 20:10
  • The regex implementation of the internal version of a word boundary is `(?:(?:^|(?<=[^a-zA-Z0-9_]))(?=[a-zA-Z0-9_])|(?<=[a-zA-Z0-9_])(?:$|(?=[^a-zA-Z0-9_])))` It uses lookbehinds which are not available in JS. You could modify it not to use lookbehinds. While your at it, modify it to not consider `-`. Good luck! ( shorthand is `(?:(?:^|(?<=\W))(?=\w)|(?<=\w)(?:$|(?=\W)))` ) –  Apr 13 '17 at 20:15
  • `\b(?<!-)\d{2}\b` ? – splash58 Apr 13 '17 at 20:23
  • @splash58: No, JS regex does not support lookbehinds. – Wiktor Stribiżew Apr 13 '17 at 20:31
  • @WiktorStribiżew you are right - I did not look at the tags :) – splash58 Apr 13 '17 at 20:32

1 Answers1

2

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]"));
Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563