JavaScript Javascript matching strings to partial matches javascript matching strings,js matching strings,javascript compare strings,javascript

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>
Answer:1

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.

Answer:2

When I try browser.ExecuteJavascript("alert('ExecuteJavaScript works!');") it works fine (pops up a alert when the browser is created). When I try browser.ExecuteJavascript("document.getElementsByName(...

When I try browser.ExecuteJavascript("alert('ExecuteJavaScript works!');") it works fine (pops up a alert when the browser is created). When I try browser.ExecuteJavascript("document.getElementsByName(...

We have a chatting website and we disabled the Copy-Paste function to prevent spammers. But now they have found a new way to use auto typing software to spam. Is there any way to detect human typing ...

We have a chatting website and we disabled the Copy-Paste function to prevent spammers. But now they have found a new way to use auto typing software to spam. Is there any way to detect human typing ...

I'm reading the documentation on Vue.extend: Create a “subclass” of the base Vue constructor. The argument should be an object containing component options. What does 'subclass' mean in Vue.js?

I'm reading the documentation on Vue.extend: Create a “subclass” of the base Vue constructor. The argument should be an object containing component options. What does 'subclass' mean in Vue.js?

  1. what does ���� mean
  2. what does smh mean
  3. what does boomer mean
  4. what does ok boomer mean
  5. what does the fox say
  6. what does simp mean
  7. what does sos mean
  8. what does dna stand for
  9. what does noel mean
  10. what does asl mean
  11. what does ig mean
  12. what does gap stand for
  13. what does otp mean
  14. what does uwu mean
  15. what does mean
  16. what does goat mean
  17. what does bomboclaat mean
  18. what does pansexual mean
  19. what does woke mean
  20. what does ion mean

Jasmine is reporting a no specs found message after adding a 14th spec to it (doesn't matter if it is a copy of a working spec). If I use a self made reporter it shows that it went through all the ...

Jasmine is reporting a no specs found message after adding a 14th spec to it (doesn't matter if it is a copy of a working spec). If I use a self made reporter it shows that it went through all the ...

  1. jasmine no specs found
  2. jasmine no specs found typescript
  3. jasmine no specs found node
  4. jasmine no specs found error
  5. jasmine specs not found
  6. jasmine incomplete no specs found
  7. jasmine-ts no specs found
  8. jasmine test no specs found
  9. jasmine karma no specs found
  10. jasmine js no specs found
  11. angular jasmine no specs found
  12. resharper jasmine no specs found
  13. jasmine spec runner no specs found
  14. protractor jasmine no specs found