简体   繁体   English

如何在split()JavaScript中排除转义字符

[英]how to exclude escaped characters in split() javascript

I am looking to ignore the characters inside the square brackets because it matches with my split parameters. 我希望忽略方括号内的字符,因为它与我的split参数匹配。

The string that i want to split is 我要拆分的字符串是

var str = "K1.1.[Other] + K1.2A.[tcc + K*-=>]";
var split = str.split(/[+|,|*|/||>|<|=|-]+/);

I want the output as K1.1.[Other], K1.2A.[tcc + K*-=>] . 我希望输出为K1.1.[Other], K1.2A.[tcc + K*-=>]

But this above code is including the characters inside square brackets which i don't want to consider. 但是上面的代码包括方括号内的字符,我不想考虑。 Any suggestion on how to solve this? 关于如何解决这个问题的任何建议?

split by both plus and braces as well. 同时用加号和大括号分开。 then go through chunks and join everything between braces pairs. 然后遍历大块,并将大括号对之间的所有内容连接起来。

But better not to use regexp at all for that. 但是最好不要完全使用regexp。

Split on the following pattern: /\\+(?![^\\[]*\\])/ 按以下模式拆分: /\\+(?![^\\[]*\\])/

https://regex101.com/r/NZKaKD/1 https://regex101.com/r/NZKaKD/1

Explanation: 说明:

\\+ - A literal plus sign \\+ -文字加号

(?! ... ) - Negative lookahead (don't match the previous character/group if it is followed by the contents of this block) (?! ... ) -负向超前(如果前一个字符/组后跟此块的内容,则不匹配)

[^\\[]* - Any number of non-left-square-brackets [^\\[]* -任意数量的非左方括号

\\] - A literal right square bracket \\] -文字右方括号

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

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