gpt4 book ai didi

javascript - 如何动态使用 .replace(/\+/g, ' ' ) 方法

转载 作者:行者123 更新时间:2023-11-30 17:47:26 26 4
gpt4 key购买 nike

我可以使用 .replace(/\+/g, ' '); jQuery 方法。

但我需要将它与动态参数一起使用。

示例:

var str1 = 'aaa';
$('#myelement').val().replace(/str1\/g,' ');

我该怎么做?

最佳答案

string.replace() 开头的是 Javascript 而不是 jQuery。有关更多信息,请查看 String.replace on MDN .

此外,string.replace() 返回一个带有替换的新字符串,并且不会影响它所针对的字符串。

您可以使用任意模式创建新的 RegExp

var regex = new RegExp('aaa', 'g');

然后做:

var replacedString = $('#myelement').val().replace(regex,' ');

获取带有替换的字符串。

所以要替换你必须做的内容:

$('#myelement').val($('#myelement').val().replace(regex,' '));

关于javascript - 如何动态使用 .replace(/\+/g, ' ' ) 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19853961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com