views:

72

answers:

1

How to replace "i" in <li>anything</li> using regex

Code :

var code = "<li>anything</li>"; // i don't know what is in <li>......</li>

result should be :

var code = "<li>anyth1ng</li>"; // i don't know what is in <li>......</li>
+6  A: 

With the information you gave us, these should work:

code = code.replace(/hi/g, "he");
code = code.replace(/i\</g, "e<");

If you have more requirements, please update the question to describe them.

Updated

var code = "<li>hi ho hi ho it's off to work we go</li>";
code = code.replace(/\>.*\</g, function(m) {
   return m.replace(/i/g, "e");
});
John Fisher
<li>anything</li> to <li>anyth!ng</li>
faressoft
The modification I just made will replace any "i" between the opening ">" and the closing "<". Will that work?
John Fisher
Is there a way more professional ? (4 lines to do it :( !!!
faressoft
What do you mean by "more professional"? You could do it in one line. (Why do you count 4? I count 3 for the actual solution.)
John Fisher
@faressoft Do you think jQuery with code being in 1 line is professional? Man I would be afraid to read your code with no line breaks.
epascarello
/[^<>]+(?=[<])/g
faressoft