javascript 查找多个字符并用 JQuery 替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9290764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:12:03  来源:igfitidea点击:

Finding multiple characters and replacing with JQuery

javascriptjqueryregex

提问by Rory Standley

I have the following JQuery AJAX call that sends HTML form data to my server side script to deal with. However I currently find line breaks and convert them to <br />for display in another part of my application. I now need to make sure I am stripping out apostrophes however I am not confident on how to replace multiple characters in one go.

我有以下 JQuery AJAX 调用,它将 HTML 表单数据发送到我的服务器端脚本进行处理。但是,我目前找到换行符并将它们转换<br />为在我的应用程序的另一部分显示。我现在需要确保我去掉了撇号,但是我对如何一次性替换多个字符没有信心。

This is the JQuery I use currently before adding this into my AJAX.

这是我在将它添加到我的 AJAX 之前当前使用的 JQuery。

description = $("#description").val().replace(/\n/g, '<br />');

Can anyone point me in the right direction?

任何人都可以指出我正确的方向吗?

Thanks

谢谢

回答by nnnnnn

You don't have to do it in one go in the sense of one call to .replace(), just chain multiple .replace()calls:

您不必一次性完成一次调用.replace(),只需链接多个.replace()调用即可:

description = $("#description").val().replace(/\n/g, '<br />')
                                     .replace(/'/g, '');

(Or replace with '&#39;'or '&apos;'if that's what you need...)

(或者替换为'&#39;'或者'&apos;'如果这就是你需要的......)

回答by T I

I agree with nnnnnn that you don't have to do it all in one go though you can use the or |regex operator and use a callback like below, see mdn regexfor more details

我同意 nnnnnn 的观点,尽管您可以使用 or |regex 运算符并使用如下所示的回调,但您不必一次性完成所有操作,有关更多详细信息,请参阅mdn regex

var foo = '"there are only 10 people in this world that understand binary, 
            \n those who can and those who can\'t"\n some joke';

foo = foo.replace(/\n|"/g, function(str) {
  if (str == '\n') {
    return '<br/>';
  } else {
    return '';
  }
});

document.write(foo);

here is a demo

这是一个演示

回答by Gerrit B

You can use | (pipe) to include multiple characters to replace:

您可以使用 | (pipe) 包含多个要替换的字符:

str = str.replace(/X|x/g, '');

(see my fiddle here: fiddle)

(在此处查看我的小提琴:fiddle

回答by sinsedrix

The .serialize()method uses encodeURIComponentto encode all of your fields in a form so you don't have to manually escape strings.

.serialize()方法用于encodeURIComponent对表单中的所有字段进行编码,因此您不必手动转义字符串。

If it's not in a form just use:

如果它不是一种形式,只需使用:

description = encodeURIComponent($("#description").val());

回答by Deepak Kumar

Try This Put your values in variables and replace.

试试这个 把你的值放在变量中并替换。

 $(function () {
            var s = "str";
            var t = "nw str";
            alert(s.replace(s, t));
        });

回答by yanisasan

you can create function recursive

您可以创建函数递归

function replaceBr(){
  var val = $("#description").val();    
  while (val !=(val = val.replace(/\n/g, '<br />')));
  return val;
}

thx

谢谢