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
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).
You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.
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).
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With