0

Assume any of the following strings were possible with ##, ?? and :: being delimiters...

test1 = 'Foo##Bar'
test2 = 'Foo Bar??Baz Mumbe'
test3 = 'SomeFoo::Some Bar'
test4 = 'Foo Bar Baz'

Now, I'd like to...

  1. know if any of ##, ?? or :: match and if they do, which one
  2. capture whatever is before and after the delimiter

String manupilation works, but just looks way too complicated.

tobibeer
  • 500
  • 3
  • 10

3 Answers3

1

Ok, I guess I actually am somewhat able to construct the RegExp...

var reg = /(.*)(\#\#|::|\?\?)(.*)/g;

Example

Now, using the exec function of a RexExp, I get...

var match = reg.exec('foo bar##baz mumble');

=> match = ["foo bar##baz mumble", "foo bar", "##", "baz mumble"];

tobibeer
  • 500
  • 3
  • 10
1

You could use something like

var m,
    before,
    after,
    delimiter,
    test = 'Foo##Bar';

if ( m = test.match( /^(.*?)(##|\?\?|::)(.*)$/ ) ) {
    before = m[1];
    delimiter = m[2];        
    after = m[3];
}

The delimiter will be whichever occurs first ##, ?? or ::.

MikeM
  • 13,156
  • 2
  • 34
  • 47
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
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87