regex - Javascript regexp capture matches delimited by character -


i have string

classifier1:11:some text1##classifier2:fdglfgfg##classifier3:fgdfgfdg##classifier4 

i trying capture terms classifier1:11, classifier2:, classifier3 , classifier4

so these classifiers can followed single semicolon or not.

so far came with

/([^#]*)(?::(?!:))/g 

but not seem capture classifier4, not sure missing here

it seems classifier in case consists of word chars may have single : in between , ends digit.

thus, may use

/(\w+(?::+\w+)*\d)[^#]*/g 

see regex demo

explanation:

  • (\w+(?::+\w+)*\d) - group 1 capturing
    • \w+ - 1 or more [a-za-z0-9_] (word) chars
    • (?::+\w+)* - 0 or more sequences of 1+ :s , 1+ word chars
    • \d - digit should @ end of group
  • [^#]* - 0 or more characters other delimiter #.

js:

var re = /(\w+(?::+\w+)*\d)[^#\n]*/g;   var str = 'classifier4##classifier1:11:some text1##classifier2:fdglfgfg##classifier3:fgdfgfdg\nclassifier1:11:some text1##classifier4##classifier2:fdglfgfg##classifier3:fgdfgfdg##classifier4';  var res = [];     while ((m = re.exec(str)) !== null) {      res.push(m[1]);  }  document.body.innerhtml = "<pre>" + json.stringify(res, 0, 4) + "</pre>";


Comments