Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex match only words starting with a specific special character

I'm trying to match only words starting with # in javascript, for eg. in the following sample text, only #these should match.

I need to match only words like #these. Ignore the ones like @#this , !#this and in#ignore.

The closer I got is here,

/(\B(#[a-z0-9])\w+)/gi

Ref: https://regex101.com/r/wU7sQ0/114

like image 930
shad Avatar asked Aug 03 '17 12:08

shad


People also ask

Can regex match special characters?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

How do I allow only special characters in regex?

You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.

What does ?= * Mean in regex?

is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

How do you search for a regex pattern at the beginning of a string?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.


1 Answers

Use a whitespace boundary (?:^|\s):

var rx = /(?:^|\s)(#[a-z0-9]\w*)/gi;
var s = "I need to match only words like #these. \nIgnore the ones like @#this , !#this and in#ignore.";
var m, res=[];
while (m = rx.exec(s)) {
  res.push(m[1]);
}
console.log(res);

Details:

  • (?:^|\s) - matches the start of string or whitespace
  • (#[a-z0-9]\w*) - Group 1 (m[1]): a #, then an alphanumeric char followed with 0 or more word chars (letters, digits, _ symbols).

See the regex demo, pay attention to what texts are captured, rather to the whole matches.

Or trimming each match:

var rx = /(?:^|\s)(#[a-z0-9]\w*)/gi;
var s = "I need to match only words like #these. \nIgnore the ones like @#this , !#this and in#ignore.";
var results = s.match(rx).map(function(x) {return x.trim();}); // ES5
// var results = s.match(rx).map(x => x.trim()); // ES6
console.log(results);
like image 79
Wiktor Stribiżew Avatar answered Oct 14 '22 03:10

Wiktor Stribiżew