简体   繁体   English

如何使用JavaScript从字符串中删除特殊字符

[英]How to remove the special characters from a string using javascript

I have the below String value to be displayed in text area and i want to remove the first characters @@*n|n from the string . 我有以下要在文本区域中显示的String值,我想从字符串中删除第一个字符@@ * n | n。

The string is as follows : Symbol-001 @@*n|nClaimant Name @@*n|nTransaction 字符串如下: Symbol-001 @@ * n | nClaimant名称@@ * n | nTransaction

I have used the below code to deal with removing the special characters 我使用下面的代码处理删除特殊字符

var paramVal1 = parent.noteText; //paramVal1 will have the string now
var pattern = /[@@*n|n]/g;
var paramVal1 = paramVal1.replace(pattern,'');
document.getElementById("txtNoteArea").value = paramval1;//appending the refined string to text area

For the above used code am getting the out put string as below 对于上面使用的代码,获取输出字符串如下所示

Symbol-001 |Claimat Name //here 'n' is missing and i have an extra '|' Symbol-001 |索赔名称 //这里'n'缺失并且我有一个额外的'|' character |Transactio //'n' is missing here too and an extra '|' 字符 | Transactio //'n'在这里也丢失了,另外一个'|' character 字符

Kindly help to remove the characters @@*n|n without affecting the other values 请帮助删除字符@@ * n | n而不影响其他值

What your regex is saying is "remove any of the following characters: @|*n ". 您的正则表达式说的是“删除以下任何字符: @|*n ”。 Clearly this isn't what you want! 显然,这不是您想要的!

Try this instead: /@@\\*n\\|n/g 请尝试以下方法: /@@\\*n\\|n/g

This says "remove the literal string @@*n|n ". 这说“删除文字字符串@@*n|n ”。 The backslashes remove the special meaning from * and | 反斜杠消除了*|中的特殊含义| .

您在模式中使用了正则表达式保留字符,需要对其进行转义。可以使用此表达式:

var pattern = /[\@\@\*n\|n]/g;

我认为使用此/ [@@ * n \\ | n] / g regEx

If you want to replace the first occurrence as you say on your question, you don't need to use regex. 如果您要替换问题中出现的第一个匹配项,则无需使用正则表达式。 A simple string will do, as long as you escape the asterisk: 只要您避开星号,一个简单的字符串就可以:

var str = "Symbol-001 @@*n|nClaimant Name @@*n|nTransaction";
var str2 = str.replace("@@\*n|n", ""); //output: "Symbol-001 Claimant Name @@*n|nTransaction"

If you want to replace all the occurrences, you can use regex, escaping all the characters that have a special meaning: 如果要替换所有出现的内容,可以使用正则表达式,转义具有特殊含义的所有字符:

var str3 = str.replace(/\@\@\*n\|n/g, ""); //output: "Symbol-001 Claimant Name Transaction"

Have a look at this regex builder, might come in handy - http://gskinner.com/RegExr/ 看看这个正则表达式生成器,可能会派上用场-http: //gskinner.com/RegExr/

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

相关问题 如何使用JavaScript从字符串中删除特殊字符 - How to remove special characters from string using javascript 如何使用Javascript从字符串中删除特殊字符 - How to remove special characters from a string using Javascript 使用 JavaScript 从字符串中删除除空格外的所有特殊字符 - Remove all special characters except space from a string using JavaScript 如何使用 JavaScript 中的正则表达式从字符串 (URL) 中删除 unicode 特殊字符 - How to remove unicode special characters from a string (URL) using regex in JavaScript 如何在Wordpress,PHP,JavaScript上使用正则表达式从字符串(URL)中删除特殊字符 - How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript 如何从字符串中删除空格和特殊字符? - How to remove spaces and special characters from string? 从 JavaScript 中的字符串中删除除 @ 符号之外的所有特殊字符 - Remove all special characters except for @ symbol from string in JavaScript Javascript从字符串的开头和结尾删除特殊字符 - Javascript remove special characters from beginning and end of string 自定义 jquery 或 javascript 从字符串扩展名中删除特殊字符 - custom jquery or javascript remove special characters from a string extension JavaScript删除特殊字符字符串不起作用 - JavaScript Remove special characters string not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM