Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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
Jquery 正则表达式不替换值_Jquery_Regex_Replace - Fatal编程技术网

Jquery 正则表达式不替换值

Jquery 正则表达式不替换值,jquery,regex,replace,Jquery,Regex,Replace,在我下面的代码中,除了$('\#output').html(value.replace(/[\(\)\{\\\\[\]\.\,\;\:\“\'']/g'))之外,一切都很正常。 不会替换这些符号,我也不确定原因。我做了一些研究,并尝试将两个替换符号连接在一起,但也没有成功 function checkValue() { var value = document.getElementById("thisinput").value; var unspeakables = ['shout', 'mes

在我下面的代码中,除了
$('\#output').html(value.replace(/[\(\)\{\\\\[\]\.\,\;\:\“\'']/g'))之外,一切都很正常。
不会替换这些符号,我也不确定原因。我做了一些研究,并尝试将两个替换符号连接在一起,但也没有成功

function checkValue() {
var value = document.getElementById("thisinput").value;
var unspeakables = ['shout', 'message'],
    formatting = { 
        'shout' : {
            'color' : 'red'
        },
    };
$('#output').html(value.replace(/[\(\)\{\}\[\]\.\,\;\:\"\']/g, '')), 
$('#output').html(value.replace(new RegExp('\\b' + unspeakables.join('\\b|\\b') + '\\b','gi'), 
function(matchedWord) {
    $('#output').css(formatting[matchedWord.toLowerCase()] || {});
    return '';
}));
我希望有人能帮助我

非常感谢。

你打电话来吗

$('#output').html(
两次。第二次,它将替换第一次设置的内容

而不是

$('#output').html(value.replace(/[\(\)\{\}\[\]\.\,\;\:\"\']/g, '')), 
$('#output').html(value.replace(new RegExp('\\b' + unspeakables.join('\\b|\\b')
...
你可能想要

value = value.replace(/[\(\)\{\}\[\]\.\,\;\:\"\']/g, '')
.replace(new RegExp('\\b' + unspeakables.join('\\b|\\b') + '\\b','gi'),
    function(matchedWord) {
        $('#output').css(formatting[matchedWord.toLowerCase()] || {});
        return '';
    }
);
$('#output').html(value);
也许你对你所做的感到困惑

value.replace(someRegex,someReplacement);

不会更改
值,因为JavaScript中的字符串是不可变的。此返回一个新字符串。

非常有用,效果非常好。感谢您的解释!(等待接受答案)。