Front-End/JavaScript
[JavaScript]자바스크립트로 텍스트 하이라이트 처리 (검색)
HMHA
2023. 2. 9. 12:26
300x250
반응형
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
위와 같은 코드에 highlight 클래스를 꾸미는 css 만 작성하면 된다.
ex) .highlight { font-weight : bold; color:#FF0000; }
In Javascript, how can I perform a global replace on string with a variable inside '/' and '/g'?
I want to perform a global replace of string using String.replace in Javascript. In the documentation I read that I can do this with /g, i.e. for example; var mystring = mystring.replace(/test/g,
stackoverflow.com
글 출처 : https://etobang.tistory.com/19
자바스크립트로 텍스트 하이라이트 처리 (검색)
var mystring = "hello world test world"; var find = "world"; var regex = new RegExp(find, "g"); mystring.replace(regex, "" + find + ""); // highlight class위와 같은 코드에 highlight 클래스를 꾸미는 css 만 작성하면 된다. ex) .highlight
etobang.tistory.com
300x250
반응형