Based off Regex Until But Not Including, I'm trying to match all characters up until a word boundary.
For example - matching apple
in the following string:
I'm doing that using:
- a negated set
[^]
- with a word boundary
\b
- and a plus
+
repeater
Like this:
/a[^\b]+/
Which should look for an "a" and then grab one or more matches for any character that is not a word boundary. So I would expect it to stop before <
which is at the end of the word
Demo in Regexr
Demo in StackSnippets
<!-- begin snippet: js hide: false --> <!-- language: lang-js -->var input = [ "apple<", "apple/" ];
var myRegex = /a[^\b]+/;
for (var i = 0; i < input.length; i++) {
console.log(myRegex.exec(input[i]));
}
<!-- end snippet -->
Couple other regex strings I tried:
I can use a negated word boundary or a negated set with a regular word boundary:
/a[\B]+/
/a[^\b]+/
I can specify several possible word ending characters and use them in a negated set:
/a[^|"<>\-\\\/;:,.]+/
I can also look for a postive set and just restrict it to return for regular letters:
/a[\w]+/
/a[a-zA-Z]+/
But I'd like to know how to do it for a word boundary if that's possible.
Here's a MDN's listing of word boundary and the characters that it constitutes