简体   繁体   English

jquery .replace()-如何替换同一行中的多个字符串?

[英]jquery .replace() - How to replace more than one string in the same line?

my name is Ahmed and I have a page that contains a textarea and a button to send data in the textarea via Ajax after clicking but I want to replace some strings before sending and the strings are   我的名字叫艾哈迈德(Ahmed),我有一个页面,其中包含一个文本区域和一个单击按钮后单击即可通过Ajax在文本区域中发送数据的按钮,但是我想在发送之前替换一些字符串,并且这些字符串是  , ­ ­ , & & , < <

and more in the JS code 以及更多JS代码

$('#CT').click(function(){
   var textarea=$('#ed_level_2');
   textarea.val(textarea.val().replace(/&/g,"").replace(/</g,"").replace(/>/g,"").replace(/"/g,"").replace(/–/g,"").replace(/—/g,"").replace(/ /g,"").replace(/ /g,"").replace(/ /g,"").replace(/­/g,"").replace(/©/g,"").replace(/™/g,"").replace(/®/g,""));
});

I am looked at some sites and I can not find a way to replace more than two strings Such as the code at the bottom 我看过一些站点,但找不到替换两个以上字符串的方法,例如底部的代码

finalurl.replace(/and/g, '&').replace(/eq/g, '=');

You can replace multiple strings at once by separating them with a vertical pipe, so this: 您可以通过用竖线将它们分开来一次替换多个字符串,因此:

$('#CT').click(function(){
   var textarea=$('#ed_level_2');
   textarea.val(textarea.val().replace(/&/g,"").replace(/</g,"").replace(/>/g,"").replace(/"/g,"").replace(/–/g,"").replace(/—/g,"").replace(/ /g,"").replace(/ /g,"").replace(/ /g,"").replace(/­/g,"").replace(/©/g,"").replace(/™/g,"").replace(/®/g,""));
});

Could be written as: 可以写成:

$('#CT').click(function(){
   var textarea=$('#ed_level_2');
   textarea.val(textarea.val().replace(/&|<|>|"|–|—| | | |­|©|™|®/g,""));
});

I'm not sure what sites you looked at, but w3schools has plenty of examples of different javascript string functions, including replace(): 我不确定您访问过哪些网站,但是w3schools提供了许多不同的JavaScript字符串函数的示例,包括replace():

https://www.w3schools.com/jsref/jsref_replace.asp https://www.w3schools.com/jsref/jsref_replace.asp

As I understand, you want to replace (remove in your case) multiple instant at once. 据我了解,您想一次替换(在您的情况下删除)多个实例。 Try this 尝试这个

textarea.val(textarea.val().replace(/\&(amp|lt|gt|quot|ndash|mdash)\;/gi,"")

better build a list 更好地建立清单

const myList = ['amp', 'lt', 'gt', 'quot', 'ndash'];
const regex = new RegExp('\&' + myList.join('|') + '\;', 'gi');
textarea.val(textarea.val().replace(regex, ''));

if you need to replace all html entity then just use regex 如果您需要替换所有HTML实体,则只需使用正则表达式

textarea.val(textarea.val().replace(/\&[a-z]+\;/gi, ''));

It depends on what problem you're trying to solve as to wether you want to replace specific entities, strip all html, or both. 这取决于您要解决什么问题,以及是否要替换特定实体,剥离所有html或两者。

 $(function() { function replaceEntities(badString) { return badString.replace(/&|<|&gt|"|–|—| | | |­|©|™|&reg/gi, ""); }; function stripHtml(badString) { return $($.parseHTML(badString)).text(); } $("form").on("submit", function(e) { e.preventDefault(); var inval = $("#in").val(); $("#out1").val(replaceEntities(inval)); $("#out2").val(stripHtml(inval)); }) }) 
 label { display: inline-block; margin: 5px; padding: 5px; border: 1px dotted #ccc; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <label>Input <textarea id="in" placeholder="try some junk html in here"></textarea> </label> <label>replaceEntities() <textarea id="out1" readonly=readonly></textarea> </label> <label>stripHTML() <textarea id="out2" readonly=readonly></textarea> </label> <input type="submit" /> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM