假设##可以使用以下任何字符串,??和::是分隔符...
test1 = 'Foo##Bar'
test2 = 'Foo Bar??Baz Mumbe'
test3 = 'SomeFoo::Some Bar'
test4 = 'Foo Bar Baz'
现在,我想......
String manupilation有效,但看起来太复杂了。
答案 0 :(得分:1)
好吧,我想我实际上有点能够构建RegExp ......
var reg = /(.*)(\#\#|::|\?\?)(.*)/g;
现在,使用RexExp的exec函数,我得到......
var match = reg.exec('foo bar##baz mumble');
=> match = ["foo bar##baz mumble", "foo bar", "##", "baz mumble"];
答案 1 :(得分:1)
您可以使用类似
的内容var m,
before,
after,
delimiter,
test = 'Foo##Bar';
if ( m = test.match( /^(.*?)(##|\?\?|::)(.*)$/ ) ) {
before = m[1];
delimiter = m[2];
after = m[3];
}
分隔符将以##
,??
或::
为准。
答案 2 :(得分:0)
test1 = 'Foo##Bar'
test2 = 'Foo Bar??Baz Mumbe'
test3 = 'SomeFoo::Some Bar'
test4 = 'Foo Bar Baz'
function match(s){
if(typeof s === "undefined") return "";
var res = /(.*)(##|\?\?|::)(.*)/g.exec(s);
return res;
}
console.log(match(test2)[1]); // before the delimiter
console.log(match(test2)[3]); // after the delimiter