[英]Replace .split() with .match() using regex in javascript
I'm having difficulties with constructing some regular expressions using Javascript. 我在使用Javascript构建某些正则表达式时遇到了困难。
What I need: 我需要的:
I have a string like: Woman|{Man|Boy}
or {Girl|Woman}|Man
or Woman|Man
etc. I need to split this string by '|' 我有一个字符串,例如:
Woman|{Man|Boy}
或{Girl|Woman}|Man
或Woman|Man
等。我需要用'|'分割此字符串 separator, but I don't want it to be split inside curly brackets. 分隔符,但我不希望在大括号内将其分隔。
Examples of strings and desired results: 字符串示例和所需结果:
// Expample 1
string: 'Woman|{Man|Boy}'
result: [0] = 'Woman', [1] = '{Man|Boy}'
// Example 2
string '{Woman|Girl}|{Man|Boy}'
result: [0] = '{Woman|Girl}', [1] = '{Man|Boy}'
I can't change "|" 我无法更改“ |” symbol to another inside the brackets because the given strings are the result of a recursive function.
括号内的另一个符号,因为给定的字符串是递归函数的结果。 For example, the original string could be
例如,原始字符串可能是
'Nature|Computers|{{Girls|Women}|{Boys|Men}}'
'自然|计算机| {{女孩|妇女} | {男孩|男人}}'
try this: 尝试这个:
var reg=/\|(?![^{}]+})/g;
Example results: 结果示例:
var a = 'Woman|{Man|Boy}';
var b = '{Woman|Girl}|{Man|Boy}';
a.split(reg)
["Woman", "{Man|Boy}"]
b.split(reg)
["{Woman|Girl}", "{Man|Boy}"]
for your another question: 对于您的另一个问题:
"Now I have another, but a bit similar problem. I need to parse all containers from the string. Syntax of the each container is {sometrash}. The problem is that container can contain another containers, but I need to parse only "the most relative" container. mystring.match(/\\{+.+?\\}+/gi); which I use doesn't work correctly. Could you correct this regex, please? "
you can use this regex: 您可以使用此正则表达式:
var reg=/\{[^{}]+\}/g;
Example results: 结果示例:
var a = 'Nature|Computers|{{Girls|Women}|{Boys|Men}}';
a.match(reg)
["{Girls|Women}", "{Boys|Men}"]
You can use 您可以使用
.match(/[^|]+|\{[^}]*\}/g)
to match those. 匹配那些。 However, if you have a nesting of arbitrary depth then you'll need to use a parser, [javascript] regex won't be capable of doing that.
但是,如果您具有任意深度的嵌套,则需要使用解析器,[javascript]正则表达式将无法做到这一点。
测试一下:
([a-zA-Z0-9]*\|[a-zA-Z0-9]*)|{[a-zA-Z0-9]*\|[a-zA-Z0-9]*}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.