8

How can I make a partial string match in Javascript?

e.g. to match 'Alf'

  • 'Alfred' -> true
  • 'Alf' -> true
  • 'alf' -> true
  • 'al' -> true
  • 'altered' -> false
  • 'half' -> false
  • '' -> false
  • 'bob' -> false

https://jsfiddle.net/zbzc5tqe/1/

I would welcome shorter / more use of javascript inbuilt functions.

var arr = ['Alfred', 'Alf', 'alf', 'al', 'altered', 'half', '', 'bob']; arr.forEach(function(element) { add(element + "->" + matches(element, 'Alf')); }); function ignoreCase(s1, s2) { var needleRegExp = new RegExp('^' + s2 + "$", "i"); return needleRegExp.test(s1) } function partializer(string) { var out = []; for (var i = 1; i < string.length; i++) { out.push(string.slice(0, i)); } return out; } function matches(text, partial) { var parts = partializer(partial); for (var i = 0; i < parts.length; i++) { if (startsWith(text, parts[i])) { return true; } } return false; } function startsWith(text, element) { var s2 = text.split(0, element.length - 1); return ignoreCase(element, s2); } function add(text) { var olList = document.getElementById('list'); var newListItem = document.createElement('li'); newListItem.innerText = text; olList.appendChild(newListItem); }
<ol id="list"> </ol>

1

3 Answers 3

9

Seems like you just want to know if a string starts with the substring:

function test(arr, sub) { sub = sub.toLowerCase(); return arr.map(str => str .toLowerCase() .startsWith(sub.slice(0, Math.max(str.length - 1, 1))) ); } var arr = ['Alfred', 'Alf', 'alf', 'al', 'altered', 'half', '', 'bob']; var results = test(arr, 'alf'); console.log(results);

Sign up to request clarification or add additional context in comments.

7 Comments

Ah yeah, I was looking at this backwards. didn't think of trimming the "input" thanks.
With the caveat that str.toLowerCase isn't internationalisation friendly.
I wasn't expecting startsWith to work this way with blank input either.
@RyanTheLeach, good catch on the internationalisation. Also, depending on your browser support, startsWith may not be supported, however, it's an easy swap to indexOf == 0.
@RyanTheLeach, I added two improvements. 1, the substring should be lowercase when checking. And 2, the sub.slice should always have a minimum of 1, otherwise just passing 'a' will produce the wrong result.
|
4

Using .indexOf to find if there is any matches in the string. Refer to

function matches(text, partial) { return text.toLowerCase().indexOf(partial.toLowerCase()) > -1; } function matchesCase(text, partial) { return text.indexOf(partial) > -1; } https://jsfiddle.net/zbzc5tqe/3/ 

Use the matchesCase() function if you would like to match case sensitive only.

3 Comments

al should be true, and half should be false.
Didn't really aware of the 'al' should be true and 'half' should be false. Agree that your answer works better if replaced with the indexOf == 0 as 'A' would be false if using the startWith method
I've edited to add an extra test case: jsfiddle.net/zbzc5tqe/5
2

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>

3 Comments

Your answer fails the following test case: 'al' -> true
sorry for a 3 years later comment, but passing in a single character as the value, such as "o" fails, it returns the empty value (from your jsFiddle example) as true, when it should be false.
@Andrew good catch ,i didn't check the all the testcases :) have made the change in the code to include the use case u have mentioned . obviously doing slice on a single charector or empty charector and subracting it with 1 will result in a empty value rather then the expected value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.