$string.replace
Replace multi-text within a portion of a string.
$string( string, replacements )
string [string]
The target string.replacements [object]
The object containing needle as name and replacement as value.
Notes
This function is useful for replacing multi-characters.
Example
HTML code
<div id="box"><div>Hi, world!</div></div> <button onclick="start_string_replace()">Start replace</button>
CSS code
#box {
width: 200px;
height: 60px;
font-size: 12px;
background: #5599bb;
padding: 10px;
}JavaScript code
function html_filter(string)
{
return $string.replace(string, {
'&': '&',
'"': '"',
"'": ''',
'<': '<',
'>': '>',
"\t": ' ',
"\n": '<br />'
});
}
function start_string_replace()
{
alert(html_filter($html($("box"))));
}
Demonstration
<div>Hi, world!</div>