JavaScript How to get rid of the empty items in match (regex operator | )?

var selectorText = "body" ; // first variant
// var selectorText = "div#id, div.body"; // multiple selector variant
// var selectorText = "div#id ul li, div.body"; // multiple selector variant
var C1 = selectorText.match(/(^|\s|,)\w+($|[\s,#\.:\[])/); // element
var C = !C1 ? 0 : C1.length;

How to make it that the result C is 1 in the case if single selector "body" and C to be 2 in the case of the multiple selector variant with the element selectors?

Current result with "body" is 3 (two empty items). I need to get rid of the empty items in the array to get correct count of elements (body, div, ul, li). I know that the empty items are created because of use the alternative operator | in brackets on begin and on end of the regex. But how to get rid of of the empty items?

Answer:1

match() returns

An Array containing the entire match result and any parentheses-captured matched results; null if there were no matches.

So you should change those capturing groups to non-capturing groups in addition to using global g modifier:

> "body".match(/(?:^|\s|,)\w+(?:$|[\s,#\.:\[])/g).length;
< 1
> "div#id, div.body".match(/(?:^|\s|,)\w+(?:$|[\s,#\.:\[])/g).length;
< 2
Answer:2

Include the g flag.

From match method description:

If the regular expression does not include the g flag, returns the same result as RegExp.exec().

Answer:3

When _.debounce is called multiple times, it only apply with the last call's argument. var f = _.debounce(function(a) { console.log(a); }) f(12) f(1223) f(5) f(42) //output -----> 42 Is ...

When _.debounce is called multiple times, it only apply with the last call's argument. var f = _.debounce(function(a) { console.log(a); }) f(12) f(1223) f(5) f(42) //output -----> 42 Is ...

  1. lodash debounce last
  2. lodash debounce cancel previous
  3. lodash debounce
  4. lodash.debounce'

I have this JQuery click event: jq("#generateButton").click({key: "hello world"}, function () { console.dir(this);// this is the "generateButton" html element. // jq(this) would ...

I have this JQuery click event: jq("#generateButton").click({key: "hello world"}, function () { console.dir(this);// this is the "generateButton" html element. // jq(this) would ...

  1. send custom data to google analytics
  2. send custom data to paypal
  3. mpi send custom data type
  4. fcm send custom data
  5. datatable send custom data
  6. ajax send custom data
  7. new relic send custom data
  8. paypal button send custom data
  9. jquery ajax send custom data
  10. push notification send custom data
  11. how to send custom data in wordpress
  12. contact form 7 send custom data

I know this is something usual, With the earlier versions of chrome I used to set "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --disable-web-security --user-data-dir in chrome ...

I know this is something usual, With the earlier versions of chrome I used to set "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --disable-web-security --user-data-dir in chrome ...

  1. google chrome wildcard certificate issue
  2. google chrome wildcard certificate
  3. google chrome wildcard search
  4. google chrome find wildcard
  5. google chrome pop up blocker wildcard

According to developers.google.com it is possible to defer the Web App Install banner, however I have only been able to cancel it completely. window.addEventListener('beforeinstallprompt', function(e)...

According to developers.google.com it is possible to defer the Web App Install banner, however I have only been able to cancel it completely. window.addEventListener('beforeinstallprompt', function(e)...