简体   繁体   English

正则表达式在方括号内得到两个特定字符

[英]Regex getting two specific characters within square brackets

Not too well-versed with Regex. 对Regex不太了解。 I have a string like this: 我有一个像这样的字符串:

 var str = "WOMEN~~Accessories >Underwear~~Socks, Tights & Leggings"

Using Javascript, I want to split at: ~~ , & , > and , including potential white space surrounding them. 我想使用Javascript分割: ~~&>,包括它们周围的潜在空白。

So far I've got: 到目前为止,我已经:

 var arr = str.split(/[\s>&,]/g);
 for (var i = 0; i<arr.length; i++){
    if(arr[i]){accepted.push(arr[i])}
 }

This doesn't account for multiple characters though (and I'm sure there's a better way to regex in the white space rather than a for loop after the fact!) and the ~~ isn't selected. 但是,这并不能解决多个字符(我相信有一种更好的方式在空白中进行正则表达式,而不是事实之后的for循环!),并且未选择~~

I'm thinking something along the lines of /[\\s>&,[~~]]/g but this doesn't work. 我在想类似/[\\s>&,[~~]]/g但这是行不通的。 How can I do this with regex? 我该如何使用正则表达式呢?

Try this: 尝试这个:

/\s*[\s>&,\[\]~]+\s*/g

Description 描述

正则表达式可视化

Demo 演示版

http://jsfiddle.net/p7FFD/ http://jsfiddle.net/p7FFD/

var arr = str.split(/(~~|[\s>&,]+)/g);

因此,您将拆分~~或空格,&符,大于(>)和逗号的任意组合。

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

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