开发者

Splitting a string in JavaScript by multiple delimeters

I want to split a string :

OU(EGAL([Zone_libre_utilisateur],"0000"),ET([Code_courrier],"ABO"))

using delimeter , & ( in ja开发者_如何学Pythonvascript. How can it be done?


You can use a regex to split:

var str = 'OU(EGAL([Zone_libre_utilisateur],"0000"),ET([Code_courrier],"ABO"))';
var a = str.split(/[,(]/);
console.log(a);
//["OU", "EGAL", "[Zone_libre_utilisateur]", ""0000")", "ET", "[Code_courrier]", ""ABO"))"]

Or even better (depending on your requirement):

var str = 'OU(EGAL([Zone_libre_utilisateur],"0000"),ET([Code_courrier],"ABO"))';
var a = str.split(/[,()[\]"]+/);
console.log(a);
//["OU", "EGAL", "Zone_libre_utilisateur", "0000", "ET", "Code_courrier", "ABO", ""]


var splitString = originalString.split(/[,(]/);


I would replace all the ( characters with , and then use the split method...

e.g. something like...

string.replace('(',',').split(',')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜