Skip to main content
added 691 characters in body
Source Link
subramanian
  • 1.3k
  • 1
  • 14
  • 12

didnt check partial usecase before, you can use regex like the one below

 var value = 'ALF'; var comparor = value.slice(0, element.length - 1); var regexp = new RegExp("^"+comparor, "i"); regexp.test(element); 

updated fiddle https://jsfiddle.net/u8jfgu3j/

var arr = ['Alfred', 'Alf', 'alf', 'al', 'half', '', 'bob']; var value = '00'; arr.forEach(function(element) { var comparor = (element.length > 1 ) ? value.slice(0, element.length - 1) : value; var regexp = new RegExp("^"+comparor, "i"); add(element + "->" + regexp.test(element)); }); function add(text) { var olList = document.getElementById('list'); var newListItem = document.createElement('li'); newListItem.innerText = text; olList.appendChild(newListItem); }
<ol id="list"> </ol>

didnt check partial usecase before, you can use regex like the one below

 var value = 'ALF'; var comparor = value.slice(0, element.length - 1); var regexp = new RegExp("^"+comparor, "i"); regexp.test(element); 

updated fiddle https://jsfiddle.net/u8jfgu3j/

didnt check partial usecase before, you can use regex like the one below

 var value = 'ALF'; var comparor = value.slice(0, element.length - 1); var regexp = new RegExp("^"+comparor, "i"); regexp.test(element); 

var arr = ['Alfred', 'Alf', 'alf', 'al', 'half', '', 'bob']; var value = '00'; arr.forEach(function(element) { var comparor = (element.length > 1 ) ? value.slice(0, element.length - 1) : value; var regexp = new RegExp("^"+comparor, "i"); add(element + "->" + regexp.test(element)); }); function add(text) { var olList = document.getElementById('list'); var newListItem = document.createElement('li'); newListItem.innerText = text; olList.appendChild(newListItem); }
<ol id="list"> </ol>

added 102 characters in body
Source Link
subramanian
  • 1.3k
  • 1
  • 14
  • 12

if its just the starting characters you want todidnt check thenpartial usecase before, you can use regex like the one below

 stringvar value = 'ALF'; var comparor = value.startsWithslice(value0, element.length - 1);  var regexp = new RegExp("^"+comparor, "i"); regexp.test(element); 

updated fiddle https://jsfiddle.net/phmbr3fw/https://jsfiddle.net/u8jfgu3j/

if its just the starting characters you want to check then you can use

 string.startsWith(value); 

updated fiddle https://jsfiddle.net/phmbr3fw/

didnt check partial usecase before, you can use regex like the one below

 var value = 'ALF'; var comparor = value.slice(0, element.length - 1);  var regexp = new RegExp("^"+comparor, "i"); regexp.test(element); 

updated fiddle https://jsfiddle.net/u8jfgu3j/

Source Link
subramanian
  • 1.3k
  • 1
  • 14
  • 12

if its just the starting characters you want to check then you can use

 string.startsWith(value); 

updated fiddle https://jsfiddle.net/phmbr3fw/