Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regEx function not does not pass test

Just wondering what could be wrong with my regEx function, is returning false, is there a better or cleaner approach to mimic startsWith as my application has an embedded JS engine that is ecma 1.8.5 spidermonkey

function startsWith(i,s) {
  var regEx = new RegExp("/^"+i+"/");
  console.log(regEx)
  return regEx.test(s);
}


const myString = "hello world";

console.log(startsWith("hello",myString));

update
Is failing to test when special characters in strings also.

startsWith("%^&.h",myString)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is a much simpler way to achieve this without regex:

function startsWith(i,s) {
  return s.indexOf(i) === 0;
}

const myString = "hello world";

console.log(startsWith("hello",myString));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading