简体   繁体   中英

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 .

The string is as follows : Symbol-001 @@*n|nClaimant Name @@*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 '|' character |Transactio //'n' is missing here too and an extra '|' character

Kindly help to remove the characters @@*n|n without affecting the other values

What your regex is saying is "remove any of the following characters: @|*n ". Clearly this isn't what you want!

Try this instead: /@@\\*n\\|n/g

This says "remove the literal string @@*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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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