简体   繁体   English

仅使用JavaScript替换一组特定的字符

[英]Replace a specific set of characters using JavaScript only

I have an RTF string that contains \\*\\revtbl{Unknown;}} , it is used to indecate that the word that follows is misspelled (I think) and I need to remove it, when I do: 我有一个包含\\*\\revtbl{Unknown;}}的RTF字符串,它用来表示后面的单词拼写错误(我认为),当我这样做时,我需要将其删除:

.replace(/{\*\revtbl{Unknown;}}/g, "")

I get two lines: 我得到两行:

*
evtbl{Unknown;}}

When I change to: 当我更改为:

.replace(/{\*\r|evtbl{Unknown;}}/g, "")

I get just the * and a second line. 我只得到*和第二行。 eg: 例如:

var tt = '\*\revtbl{Unknown;}}';
tt=tt.replace(/{\*\r|evtbl{Unknown;}}/g, "");
alert('"'+tt+'"');

I see: 我懂了:

"*
"

I could not find any reference about the use of the pipe | 我找不到有关管道使用的任何参考| character so I need to know: if by using it am I asking to replace two separate strings one being {\\*\\r and the other being evtbl{Unknown;}} bottom line I need to replace the literal string \\*\\revtbl{Unknown;}} with nothing. 字符,因此我需要知道:如果要使用它,我要替换两个单独的字符串,一个是{\\*\\r ,另一个是evtbl{Unknown;}}底线,我需要替换文字字符串\\*\\revtbl{Unknown;}}一无所有。

I think it is just a matter of escaping all the characters correctly 我认为这只是正确转义所有字符的问题

//sample string - NOTE: the backslashes had to be escaped for JS
var str = "RTF string that contains \\*\\revtbl{Unknown;}}, it is used to indecate that the word that follows is misspelled (I think) and I need to remove it, when I do:";

var regEx = /\\\*\\revtbl\{Unknown;\}\}/g;

console.log(str.replace(regEx, ''));

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

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