Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解释奇怪的jquery选择器_Javascript_Jquery - Fatal编程技术网

Javascript 解释奇怪的jquery选择器

Javascript 解释奇怪的jquery选择器,javascript,jquery,Javascript,Jquery,我在完成练习“将h2更改为h3”时进行了练习,然后按“放弃”查看答案,我看到了以下内容: var target = $('#target'); target.html(target.html().replace(/h2/g,'h3')); 有人知道这个.replace(/h2/g,'h3')“选择器”是如何工作的吗 我没有找到关于.replace()的任何内容,可能已弃用。但即使在找到.replaceWith()时,我也没有找到任何对该选择器的引用,这些引用看起来像带有转义字符的reg-ex

我在完成练习“将h2更改为h3”时进行了练习,然后按“放弃”查看答案,我看到了以下内容:

var target = $('#target');
target.html(target.html().replace(/h2/g,'h3'));
有人知道这个.replace(/h2/g,'h3')“选择器”是如何工作的吗

我没有找到关于.replace()的任何内容,可能已弃用。但即使在找到.replaceWith()时,我也没有找到任何对该选择器的引用,这些引用看起来像带有转义字符的reg-ex


如果你能解释一下或者指出一些可以阅读的东西,我的分数就满了。

事实上,如果我是正确的/h2/是正则表达式。因此,在//之间的任何内容都将被替换为逗号后的字符串。g是全局应用匹配的标志


您可以在

上准确地了解它,它不是jQuery选择器,而是javascript替换的常规功能


其中,作为第一个参数的/h2/g只是一个正则表达式,基本上,
target.html()
将返回一个字符串,然后
string.replace
part将执行regex replace并返回一个字符串。然后
target.html(string)
将目标的outerhtml设置为修改后的

replace
是javascript
String
对象的一种方法-我知道了。所以这不是jquery,而是带有正则表达式的纯JavaScript。谢谢你的帮助。