JavaScript - 在RegExp中用“/”替换“\”字符的正确方法是什么

时间:2021-02-23 19:13:13

I defined a function in JavaScript that replace all -, _, @, #, $ and \ (they are possible separators) with / (valid separator).

我在JavaScript中定义了一个函数,用/(有效分隔符)替换所有 - ,_,@,#,$和\(它们是可能的分隔符)。

My goal is any string like "1394_ib_01#13568" convert to "1394/ib/01/13568"

我的目标是任何字符串,如“1394_ib_01#13568”转换为“1394 / ib / 01/13568”

function replaceCharacters(input) {

    pattern_string = "-|_|@|#|$|\u005C";      // using character Unicode
    //pattern_string = "-|_|@|#|$|\";         // using original character
    //pattern_string = "-|_|@|#|$|\\";        // using "\\"
    //pattern_string = "\|-|_|@|#|$";         // reposition in middle or start of string
    pattern = new RegExp(pattern_string, "gi");

    input = input.replace(pattern, "/");
    return input;
}

My problem is when a string with \ character send to function result is not valid.

我的问题是当一个带有\字符发送到函数结果的字符串无效时。

I tried use Unicode of \ in define pattern, Or use \\\ instead of it. Also I replaced position of it in pattern string. But in any of this situation, problem wasn't solved and browser return invalid result or different error such as:

我尝试在定义模式中使用\的Unicode,或者使用\\\而不是它。我也用模式字符串替换了它的位置。但在任何这种情况下,问题都没有解决,浏览器返回无效结果或不同的错误,如:

SyntaxError: unterminated parenthetical    ---> in using "\u005C"
SyntaxError: \ at end of pattern           ---> in using "\\"
Invalid Result: broken result in 2 Line or replace with undefined character based on input string (the character after "\" determine result)
                        ---> in reposition it in middle or start of pattern string

1 个解决方案

#1


8  

var pattern_string = "-|_|@|#|\\$|\\\\";

You have to escape the slash once for the pattern, so it'll try to match the literal character:

你必须为模式转义一次斜杠,所以它会尝试匹配文字字符:

\\

Then, escape each slash again for the string literal:

然后,再次为字符串文字转义每个斜杠:

"\\\\"

Also note that I added an escape for the $. To match a dollar sign literally, it'll needs to be escaped as well, since it normally represents an anchor for the "end of the line/string."

另请注意,我为$添加了一个转义符。为了按字母顺序匹配美元符号,它也需要进行转义,因为它通常代表“行/字符串结尾”的锚点。


You can also use a Regex literal to avoid the string, using only the escape sequences necessary for the pattern:

您还可以使用正则表达式文字来避免字符串,仅使用模式所需的转义序列:

var pattern = /-|_|@|#|\$|\\/gi;

And, as you're matching only single characters, you can use a character class instead of alternation:

而且,由于您只匹配单个字符,因此您可以使用字符类而不是替换字符:

var pattern = /[-_@#\$\\]/gi;

(Just be careful with the placement of the - here. It's fine as the first character in the class, but can represent a range of characters when placed in the middle. You can also escape it to ensure it doesn't represent a range.)

(请注意 - 这里的位置。它可以作为类中的第一个字符,但是当放在中间时可以表示一系列字符。您也可以将其转义以确保它不代表范围。 )

#1


8  

var pattern_string = "-|_|@|#|\\$|\\\\";

You have to escape the slash once for the pattern, so it'll try to match the literal character:

你必须为模式转义一次斜杠,所以它会尝试匹配文字字符:

\\

Then, escape each slash again for the string literal:

然后,再次为字符串文字转义每个斜杠:

"\\\\"

Also note that I added an escape for the $. To match a dollar sign literally, it'll needs to be escaped as well, since it normally represents an anchor for the "end of the line/string."

另请注意,我为$添加了一个转义符。为了按字母顺序匹配美元符号,它也需要进行转义,因为它通常代表“行/字符串结尾”的锚点。


You can also use a Regex literal to avoid the string, using only the escape sequences necessary for the pattern:

您还可以使用正则表达式文字来避免字符串,仅使用模式所需的转义序列:

var pattern = /-|_|@|#|\$|\\/gi;

And, as you're matching only single characters, you can use a character class instead of alternation:

而且,由于您只匹配单个字符,因此您可以使用字符类而不是替换字符:

var pattern = /[-_@#\$\\]/gi;

(Just be careful with the placement of the - here. It's fine as the first character in the class, but can represent a range of characters when placed in the middle. You can also escape it to ensure it doesn't represent a range.)

(请注意 - 这里的位置。它可以作为类中的第一个字符,但是当放在中间时可以表示一系列字符。您也可以将其转义以确保它不代表范围。 )