我想匹配这些字符:[]()在javascript中的正则表达式中的字符类中,我该怎么做?
grep中的解决方案是: (regex - brackets and parenthesis in character-class)
<script>
var text = "echo some text (some text in parenthesis) [some other in brackets]";
var patt = /[][()]/g
console.log(text.match(patt));
</script>
但是这个正则表达式在JS中没有提供匹配
答案 0 :(得分:0)
你应该转义正则表达式模式中的方括号:
var text = "echo some text (some text in paranthesis) [some other in brackets]",
patt = /[\]\[()]/g;
console.log(text.match(patt)); // ["(", ")", "[", "]"]
答案 1 :(得分:0)