简体   繁体   中英

Regex getting two specific characters within square brackets

Not too well-versed with 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.

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.

I'm thinking something along the lines of /[\\s>&,[~~]]/g but this doesn't work. How can I do this with regex?

Try this:

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

Description

正则表达式可视化

Demo

http://jsfiddle.net/p7FFD/

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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