javascript - Using regular expressions to split on = , == , and === separately -
i having trouble code trying split string input , save different lists splitting on = , ==, , === each list splits on corresponding one
var tokenequals = code.split(/[=]/gi); var tokendoubleequals = code.split(/[==]/gi); var tokentrippleequals = code.split(/[===]/gi);
so if if code "= == ===" each list should have length of 2
but happening is splitting on every = , of them end same amount of 7
i have refined down
var tokenequals = code.split(/\=(?!\=)/); var tokendoubleequals = code.split(/\=\=(?!\=)/gi); var tokentrippleequals = code.split(/\=\=\=/gi);
the current output is: equals length 4, double equals length 3, , tripple equals length 2
when should equal 1
so triple 1 working fine other 2 not. wondering solution put in regexp fields split on cause them split on correspond amount of equals.
bellow example of code in action returns [3, 2, 1] when want return [1, 1, 1]
var lloccounter = function() {}; lloccounter.prototype.count = function(code) { var numberoflloc = []; code = code.trim(); var tokenequals = code.split(/\=(?!\=)/); var tokendoubleequals = code.split(/\=\=(?!\=)/gi); var tokentrippleequals = code.split(/\=\=\=/gi); numberoflloc = [ tokenequals.length - 1, // 4 = tokendoubleequals.length - 1, // 5 == tokentrippleequals.length - 1, // 6 === ] console.log(numberoflloc) }; contents = "= == ===" lloccounter = new lloccounter(); numberoflloc = lloccounter.count(contents);
i suggest using mimicking negative lookbehind technique described in mimicking lookbehind in javascript article.
the idea pre-process symbol(s) need split against can use in split
method. suggest using literals _single_
or similar:
code = code.trim().replace(/(=)?=(?!=)/g, function($0, $1){ // pre-process single = return $1 ? $0 : '_single_'; }); code = code.replace(/(=)?==(?!=)/g, function($0, $1){ // pre-process double = return $1 ? $0 : '_double_'; }); code = code.replace(/(=)?===(?!=)/g, function($0, $1){ // pre-process triple = return $1 ? $0 : '_triple_'; }); var tokenequals = code.split("_single_"); // split temp tokens var tokendoubleequals = code.split("_double_"); var tokentrippleequals = code.split("_triple_");
so, basically, how works: (=)?=(?!=)
contains optional capturing group in front ((=)?
) can capture =
or empty, =
matched (in other 2 patterns, ==
or ===
respectively), , no =
should follow (thanks negative lookahead (?!=)
). inside replace
method, callback used analyze match data structure: if group 1 (that optional) matched, means =
present before =
(or ==
, or ===
) , not want match =
(or ==
or ===
). $1 ? $0 : '_single_'
piece of code that: $0
stands whole match (if $1
(i.e. group 1 value) not empty, reinsert text found, else, replace temporary token.
var lloccounter = function() {}; lloccounter.prototype.count = function(code) { var numberoflloc = []; code = code.trim().replace(/(=)?=(?!=)/g, function($0, $1){ // pre-process single = return $1 ? $0 : '_single_'; }); code = code.replace(/(=)?==(?!=)/g, function($0, $1){ // pre-process double = return $1 ? $0 : '_double_'; }); code = code.replace(/(=)?===(?!=)/g, function($0, $1){ // pre-process triple = return $1 ? $0 : '_triple_'; }); var tokenequals = code.split("_single_"); // split temp tokens var tokendoubleequals = code.split("_double_"); var tokentrippleequals = code.split("_triple_"); numberoflloc = [ tokenequals.length - 1, // 4 = tokendoubleequals.length - 1, // 5 == tokentrippleequals.length - 1, // 6 === ] console.log(numberoflloc) }; contents = "= == ===" lloccounter = new lloccounter(); numberoflloc = lloccounter.count(contents);
Comments
Post a Comment