js regular expression parsing of solving html <> tag problem

Scenario:
to blog writing, for example, sometimes we inadvertently write the string with a label, then the browser will parse it, in fact we do not want it to be resolved, so the core code can resolve the problem.

The core code is as follows:

data.codeSource.replace(new RegExp("<","g"),"&lt;").replace(new RegExp(">","g"),"&gt;")

data.codeSource here is equivalent to get back to interact with the data

js new RegExp is regular expression object

replace method, a total of two parameters, one is the original string, the other is a new string

replace(old_string,new_string)

Guess you like

Origin www.cnblogs.com/youcong/p/11440591.html