Count matches at the end of a string

314 views Asked by At

I want to count the number of occurrences of a string at the end of the string. I want it to ignore occurrences that are not at the end and I want it to return zero if there are none.

terminal string to find: "-1"

e.g. string = 1
// 0
e.g. string = 1-1
// 1
e.g. string = 1-1-1
// 2
e.g. string = 1-2-1
// 1
e.g. string = 1-2-1-2-1
// 1
e.g. string = 2-1-1-1-1
// 4
e.g. string = 1-1-2
// 0

I want it to count all of the occurrences at the end of the string, not just the occurrences anywhere in the string.

I tried:

var occurrences = string.match(/(-1)*$/).length;

I have a JS Fiddle here: JS Fiddle

It returns "2", no matter what my string.

5

There are 5 answers

1
n4m31ess_c0d3r On BEST ANSWER

You need to work on the first element of the returned array:

string.match(/(-1)*$/)[0].length/2

Find the length of first element which is the matched string of "-1" at the end and divide by 2 since the string "-1" is of length 2.

To quote from MDN String.prototype.match():

Return value

If the string matches the expression, it will return an Array containing the entire matched string as the first element, followed by any results captured in parentheses. If there were no matches, null is returned.

function count() {
  console.log(string.match(/(-1)*$/)[0].length / 2)
}

var string = "1"
// 0
count();

string = "1-1"
// 1
count();

string = "1-1-1"
// 2
count();

string = "1-2-1"
// 1
count();

string = "1-2-1-2-1"
// 1
count();

string = "2-1-1-1-1"
// 4
count();

string = "1-1-2"
// 0
count();

0
Alex K. On

Walk backwards and count?

var string = "2-1-1-1-1";
var find =   "-1";
var match =  0;

for (var i = string.length; i >= 0; i -= find.length) {
  if (string.substr(i - find.length, find.length) == find)
    match++;
  else
    break;
}

console.log("="+match)

0
Muhammad Usman On

Try this. This runs Ok for all of your tested values

function countOnesAtTheEnd(s){
var array = s.split("-");

var count = 0;
for(var i =1; i<array.length; i++)
{ 
if(array[i] =="1"){
 count++;
 }
 else count = 0;

}
console.log(count);
}
countOnesAtTheEnd("1");
countOnesAtTheEnd("1-1");
countOnesAtTheEnd("1-1-1");
countOnesAtTheEnd("1-2-1");
countOnesAtTheEnd("1-2-1-2-1");
countOnesAtTheEnd("2-1-1-1-1");
countOnesAtTheEnd("1-1-2");

3
Ori Drori On

Use String#match to find the chain of -1 that reaches the end, then split by -1, and get the length -1. When you split an a string of -1-1-1 you'll get an array with four items. Now we can extract the length, and reduce it by one.

function m(str) {
  return str.match(/(-1)*$/g)[0].split(-1).length - 1;
}

console.log(m('1')) // 0
console.log(m('1-1')) // 1
console.log(m('1-1-1')) // 2
console.log(m('1-2-1')) // 1
console.log(m('1-2-1-2-1')) // 1
console.log(m('2-1-1-1-1')) // 4
console.log(m('1-1-2')) // 0

1
Jonas Wilms On

Non regex version:

var occurences = 0;
string.split("-1").reduceRight((exit, space) => {
  if(exit || space) return true;
  occurences++;
  return false;
}, false);