Reputation: 11
I have a regular expression pattern: [a-zA-Z0-9]{8}
.
Is there some way to negate this pattern? I mean that using that expression I should be able to match all substrings that do not match this pattern.
I tried a negative look behind - (?!(a-zA-Z0-9{8}))
, but that has never worked in JavaScript.
Upvotes: 0
Views: 98
Reputation: 626802
In order to obtain all the substrings that do not match a specific pattern, you can use String#split
:
var re = /[a-zA-Z0-9]{8}/;
var s = "09 ,Septemeber";
document.body.innerHTML += JSON.stringify(s.split(re).filter(Boolean));
The idea is that those substrings that match will be delimiters, and will be missing from the obtained array.
I added .filter(Boolean)
to get rid of empty array elements that often appear when splitting with a regex.
Note that the pattern should not contain capturing groups, or the captured substrings will be part of the resulting array.
Upvotes: 3