1

I've got

mystring = "Google" and I've got array

myarray = ["o", "o", "e"]

I want to do something like

mystring.replace(myarray, "<b>$</b>") 

So it would return G<b>o</b><b>o</b>gl<b>e</b> - every comming matching letter from array is wrapped in tag.

I also have proper regexp for it /.*?(o).*?(o).*(e).*/i That have matching groups for o followed by o followed by e.

Those arrays and strings are auto generated (fuzzy search) so I'm not able to say how big array and strings will be. I only know the letters to look in the string.

2
  • 1
    The match would be for only the first ocurrency of each element of the array? I ask because you don't need two o in your array if not. Commented Jun 27, 2014 at 11:24
  • I need to have two o in array as it says me I need to 'bold' two o's in string not only one. And also I need to respect order of letters in array. So first o means 'find firs o in string' and then 'find another o but it has to be after last match from array' Commented Jun 27, 2014 at 11:26

3 Answers 3

2

You can do:

mystring =  "Google";
myarray = ["o", "o", "e"];

var r = mystring.replace(new RegExp( '(' + myarray.join('|') + ')', 'g'), "<b>$1</b>");
//=> G<b>o</b><b>o</b>gl<b>e</b>

EDIT: Based on discussions below:

mystring =  "Google";
myarray = ["g", "o", "e"];

var r = mystring;
for (var i in myarray) {
    r = r.replace(new RegExp('('+myarray[i]+')(?!<\\/b>)', "i"), "<b>$1</b>");
}
console.log(r); // <b>G</b><b>o</b>ogl<b>e</b>

PS: Due to use of negative lookahead (?!<\/b>) same letter is not replaced twice.

JSFiddle

Sign up to request clarification or add additional context in comments.

12 Comments

Lets say array would be ["g", "o", "e"] - then what should happen is: g, first o (only the first, not every) after found g and firs e after found o should be 'bolded'. btw. I'm not the one who voted down.
1+ It is exactly what I did at fiddle. I would like to know what is wrong too.
Can you supply fiddle? here is mine : jsfiddle.net/8fTM8 (two o's bolded, should be only the first after g). Same with g's - only the first in order should be bolded
Here's mine jsfiddle.net/8fTM8 - expected result, first G, first o after last match, first e after last match to be bold. Actual result - every g, o, e bolded.
For the last comment of the OP still wont work. You have to add ignorecase RegExp('('+myarray[i]+')','i')
|
1

Here is my way to go:

var tmp = mystring;
for (l in myarray) {
    tmp = tmp.replace(new RegExp('('+myarray[l]+'(?:\\B|$))'), '<b>$1</b>');
}

Console:

G<b>o</b><b>o</b>gl<b>e</b>

The (?:\\B|$) makes the regex not producing G<b><b>o</b></b>ogl<b>e</b>

You could also use:

new RegExp('('+myarray[l]+'(?!<))')

Comments

0

So I came up with something like this:

var boldByArray = function( string, array ){
    var result = [];
    string.split('').forEach( function( letter ){
        if( array.indexOf( letter ) == 0 ){
             result.push( '<b>' + array.shift() +'</b>' );
        }
        else {    
            result.push( letter );
         }
    });
    return result.join('');
}

The result for array boldByArray('google', ['e','g','o']) will be Googl<b>e</b> because you want to respect the order, so it finds the e, then looks for g and o after that e which never comes. boldByArray('google', ['x','g','o']) will be just google, because there's no x.

http://jsfiddle.net/wuSaE/1/

Updated with fuzzy matching demo gathered from comments you've provided: http://jsfiddle.net/wuSaE/3/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.