Vikash Vikash - 9 months ago 40
Node.js Question

What will be the regular expression for below requirement in javascript


any word that start with a and end with b having middle char digit. this word should not be on the line which start with char '#'

Given string:

a1b a2b a3b
#a4b a5b a6b
a7b a8b a9b

Expected output:


regex: ?i need it for javascipt.

So far tried below thing:

var text_content =above_mention_content
var reg_exp = /^[^#]?a[0-9]b/gmi;
var matched_text = text_content.match(reg_exp);

Getting below output:

[ 'a1b', ' a7b' ]

Answer Source

Your /^[^#]?a[0-9]b/gmi will match multiple occurrences of the pattern matching the start of line, then 1 or 0 chars other than #, then a, digit and b. No checking for a whole word, nor actually matching words farther than at the beginning of a string.

You may use a regex that will match lines starting with # and match and capture the words you need in other contexts:

var s = "a1b a2b a3b\n#a4b a5b a6b\n a7b a8b a9b";
var res = [];
s.replace(/^[^\S\r\n]*#.*|\b(a\db)\b/gm, function($0,$1) {
  if ($1) res.push($1);

Pattern details:

  • ^ - start of a line (as m multiline modifier makes ^ match the line start)
  • [^\S\r\n]* - 0+ horizontal whitespaces
  • #.* - a # and any 0+ chars up to the end of a line
  • | - or
  • \b - a leading word boundary
  • (a\db) - Group 1 capturing a, a digit, a b
  • \b - a trailing word boundary.

Inside the replace() method, a callback is used where the res array is populated with the contents of Group 1 only.