我的英语不好。抱歉!
如何在一个变量内用双引号写引号?
我想合并两个脚本:number1和number2
//number 1:
var rew = /\'/g;
$('span').contents().each(function() {
if (this.nodeType === 3 && rew.test(this.nodeValue)) {
this.nodeValue = this.nodeValue.replace(rew, '');
}
});
//number 2:
var ref = /\"/g;
$('span').contents().each(function() {
if (this.nodeType === 3 && ref.test(this.nodeValue)) {
this.nodeValue = this.nodeValue.replace(ref, '');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span>
Hello 'world'.<br>
Hello "world".
</span>
一方面,您可以简单地在字符集中同时包含'
和"
:
/['"]/g
这将匹配并删除所有引号。但这也会删除实际上没有引用的引号,例如:
Bob's
games' difficulty
因此,您可以考虑在分词符之前仅匹配具有匹配引号的引号。匹配并捕获a'
或a "
,然后重复并捕获(在第二个捕获组中)任何非空格字符,直到遇到相同的捕获字符,然后替换为第二个捕获组(用引号引起来的非空格字符):
//number 1:
var rew = /(['"])(\S+)\1/g;
$('span').contents().each(function() {
if (this.nodeType === 3) {
this.nodeValue = this.nodeValue.replace(rew, '$2');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span>
Hello 'world'.<br>
Hello "world".<br>
Bob's<br>
games' difficulty
</span>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句