반응형
var mystring = "hello world test world";
var find = "world";
var regex = new RegExp(find, "g");
mystring.replace(regex, "<span class='highlight'>" + find + "</span>"); // highlight class
"hello <span class='highlight'>world</span> test <span class='highlight'>world</span>"

"g"옵션으로 글로벌하게 모든 부분을 변경할 수 있다.

 

( .highlight { font-weight : bold; color:#FF0000; } 를 css에 넣어 highlight에 효과주기)

 

또다른 regex를 쓰지 않고 할수 있는 팁을 주자면

 

 

mystring.split("world").join("<span class='highlight'>world</span>")
"hello <span class='highlight'>world</span>  test <span class='highlight'>world</span> "

와같은 방식도 동일하게 효과를 낸다.

 

 

 

 

반응형