-1

I'd like to compare a string in JavaScript using a wildcard such that I could compare whether two strings are true but using a wildcard. For example, I might have inputs like this:

const input1 = "300";
const input2 = "300 - Item#300";

I'd want to be able to construct a function so that this statement would return true:

input1 === input2;

I found this question/answer: Wildcard string comparison in Javascript

However, the tool that I'm using is not using all of JavaScript ES6 and so I don't have all the tools needed in the highest voted answer. I should have everything found in the previous version (we apparently have some Frankenstein version of ES6 where we only have parts of it)..l

JohnN
  • 968
  • 4
  • 13
  • 35
  • why are both strings equal? – Nina Scholz Mar 11 '20 at 14:04
  • what's wrong with this solution? https://stackoverflow.com/a/26246662/4733161 – cloned Mar 11 '20 at 14:05
  • Probably because I can never seem to understand Regex. I haven't been able to get that one to work. It looks like to invoke a regular expression in Javascript, I need to wrap the text in `/` like `/300/`. So I tried writing /300*/ == /300 - Item#300/ but it returns false. – JohnN Mar 11 '20 at 14:23

1 Answers1

1

If you like to check if a sting contains another string, you could take String#includes without using a regular expression.

const
    input1 = "300",
    input2 = "300 - Item#300";

console.log(input2.includes(input1)); // true
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This works but order doesn't seem to matter which is a downside. I did find that I can use `.indexOf()` to see if something is in a string. It will return `-1` if it is not there. – JohnN Mar 11 '20 at 16:58
  • for other checks, you could take [`String#startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) or [`String#endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). – Nina Scholz Mar 11 '20 at 17:17