With a JS Regex matching exact word but not hypenated words starting with
said word
I could not find a match to this question.
I have a string like so
var s="one two one-two one-three one one_for"
and my function is as follows
function replaceMatches( str, word )
{
var pattern=new RegExp( '\\b('+word+')\\b','g' )
return str.replace( pattern, '' )
}
the problem is if I run the function like
var problem=replaceMatches( s,'one' )
it
returns two -two -three one_four"
the function replaces every "one" like it should but treats words with a
hyphen as two words replacing the "one" before the hyphen.
My question is not about the function but about the regex. What literal
regex will match only the words "one" in my string and not "one-two" or
"one-\w"<--you know what I mean lol
basically
var pat=/\b(one)\b/g
"one one-two one".replace( pat, '')
only replace the exact match "one" and not the one in "one-two" the "one"
on the end is important to, the regex must work if the match is at the
very end Thank you, sorry if my question is relatively confusing. I am
just trying to get my learn on, and expand my personal library.
No comments:
Post a Comment