87,996
社区成员




var expression = new RegExp('(<(?:"[^"]*"|\'[^\']*\'|[^"\'>]+)+>)|(?:(?!<\\/?\\w)[\\S\\s])+', 'g');
<textarea>
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,'over')" onmouseout="' +this.name+'.dddd(this,'out')">abcdefgada </div>
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,'over')" onmouseout="' +this.name+'.dddd(this,'out')">
abcde
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,'over')" onmouseout="' +this.name+'.dddd(this,'out')">abcdefgada </div>
fgada </div>
</textarea>
<textarea style="width:700px; height:500px;">
</textarea>
<script type="text/javascript">
var replaceInTextNode = function () {
var expression = /(<(?:"[^"]*"|'[^']*'|[^"'>]+)+>)|(?:(?!<\/?\w)[\S\s])+/g;
return function (text, handler) {
return String(text).replace(expression, function (lastMatch, $1) {
return $1 ? $1 : handler(lastMatch);
});
};
}();
var key = 'c';
document.getElementsByTagName('textarea')[1].value = replaceInTextNode(document.getElementsByTagName('textarea')[0].value, function (v) {
alert(v);
return v.replace(new RegExp('(' + key + ')', 'g'), '<strong>$1</strong>');
});
</script>
<textarea>
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,'over')" onmouseout="' +this.name+'.dddd(this,'out')">abcdefgada </div>
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,'over')" onmouseout="' +this.name+'.dddd(this,'out')">
abcde
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,'over')" onmouseout="' +this.name+'.dddd(this,'out')">abcdefgada </div>
fgada </div>
</textarea>
<textarea style="width:700px; height:500px;">
</textarea>
<script>
var replaceInTextNode = function () {
var expression = /(<(?:"[^"]*"|'[^']*'|[^"'>]+)+>)|(?:(?!<\w).)+/g;
return function (text, handler) {
return String(text).replace(expression, function (lastMatch, $1) {
return $1 ? $1 : handler(lastMatch);
});
};
}();
var key = 'c';
document.getElementsByTagName('textarea')[1].value = replaceInTextNode(document.getElementsByTagName('textarea')[0].value, function (v) {
return v.replace(new RegExp('(' + key + ')', 'g'), '<strong>$1</strong>');
});
</script>