-4

Related (but not the same):

Javascript Regex: How to bold specific words with regex?

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 and I want special characters (such as @!#.()) to be ignored in the search

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?

I've got this currently:

function updateHaystack(input, needle) {
    return input.replace(new RegExp('(^|\\s)(' + needle + ')(\\s|$)','ig'), '$1<b>$2</b>$3');
}

unfortunately it doesn't bold words that are concatenated with a special char... eg. !cow does not turn into !<b>cow</b>

Thank you

Community
  • 1
  • 1
rawrrrrrrrr
  • 3,647
  • 7
  • 28
  • 33
  • 4
    Why are you continuously reposting this: http://stackoverflow.com/questions/1243321/javascript-regex-add-bold-tags-around-needles-in-a-haystack – Amber Aug 08 '09 at 00:52
  • What are you doing? http://stackoverflow.com/questions/1230445/javascript-regex-how-to-bold-specific-words-with-regex – Sinan Ünür Aug 08 '09 at 00:58
  • because i've altered the question – rawrrrrrrrr Aug 08 '09 at 01:15
  • 4
    inktri. You need to stop re-posting the same question. You asked it here (http://stackoverflow.com/questions/1230445) two days ago, got a good answer AND marked it as accepted. If you have a sublety or nuance you are trying to get clarification on, describe it as such. Otherwise, your questions will continue to be down-voted and closed. – Peter Bailey Aug 08 '09 at 01:15
  • i just realized the examples are wrong... and i've altered the question as such. what am i supposed to do? just edit the original question? or do i repost it with the altered examples, etc. ? – rawrrrrrrrr Aug 08 '09 at 01:18
  • 2
    when you post a new question related to a previous one, do two things: 1. Provide a link to the original question. 2. Be clear and explicit about the ways in which this new question is different. – Peter Bailey Aug 08 '09 at 01:39
  • Or just ask in the comments of the original thread for a little additional help. Honestly, you're just copying and pasting the same text for all of these questions. – womp Aug 08 '09 at 06:13

4 Answers4

1

It sounds like \b should do what you want. It's a zero-width match of "word boundaries".

function updateHaystack(input, needle) {
    return input.replace(new RegExp('\\b(' + needle + ')\\b','ig'), '<b>$1</b>');
}
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
1

If you want to allow punctuation to be part of the needle, just add it:

function updateHaystack(input, needle) {
    return input.replace(new RegExp('(^|\\s)(' + needle + '[!.,?]?)(\\s|$)','ig'), '$1<b>$2</b>$3');
}
Amber
  • 507,862
  • 82
  • 626
  • 550
1

Allow any non-whitespace character to be part of the needle:

"cows, at www.cows.com, milk COWS!".replace(/(\s|^)(cow\S*)(\s|$)/ig, '$1<b>$2</b>$3');
// Will return: "<b>cows,</b> at www.cows.com, milk <b>COWS!</b>"
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
0

So you need to put an optional section for the special char in the regex:

'(^|\\s)(' + needle + '[@!#.(),?]*)(\\s|$)'
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137