Given a needle and a haystack... I want to put bold tags around the needle. So what regex expression would I use with replace()? I want SPACE to be the delimeter and I want the search to be case insensitive
so say the needle is "cow" and the haystack is
cows at www.cows.com, milk some COWS
would turn into
<b>cows</b> at www.cows.com, milk some <b>COWS</b>
also keywords should be able to have spaces in it so if the keyword is "who is mgmt"...
great band. who is mgmt btw?
would turn into
great band. <b>who is mgmt</b> btw?
Thanks
The RegExp \B Metacharacter in JavaScript is used to find a match which is not present at the beginning or end of a word. If a match is found it returns the word else it returns NULL. Syntax: /\B/ or new RegExp("\\B") Syntax with modifiers: /\B/g.
The backslash in combination with a literal character can create a regex token with a special meaning. E.g. \d is a shorthand that matches a single digit from 0 to 9. Escaping a single metacharacter with a backslash works in all regular expression flavors.
For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
Java - String matches() Methodmatches(regex) yields exactly the same result as the expression Pattern.
For those who don't want SPACE as the delimiter, simply don't use \s
.
function updateHaystack(input, needle)
{
return input.replace(new RegExp('(^|)(' + needle + ')(|$)','ig'), '$1<b>$2</b>$3');
}
Worked for me.
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