9

Is there a function in javascript that compares a string and returns a boolean? I found .match but it returns the strings that matched. I was hoping there was something else so that I would have a lesser code in comparing a string. Since I wanted to check if a string has this word and proceed else not.

thanks

2
  • are you looking for an exact string1 to string2 match, or is string1 something that can occur somewhere within a larger string2? Commented May 22, 2012 at 15:32
  • that was my first solution but i encountered a flaw. Email == Friend Email will return false. so i was looking for a strcmp like function for javascript. :) Commented May 22, 2012 at 15:33

8 Answers 8

32

You can use the RegEx test() method which returns a boolean:

/a/.test('abcd'); // returns true.
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer. "The test() method executes a search for a match between a regular expression and a specified string. Returns true or false." -- developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
15

You may use type augmentation, especially if you need to use this function often:

String.prototype.isMatch = function(s){
   return this.match(s)!==null 
}

So you can use:

var myBool = "ali".isMatch("Ali");

General view is that use of type augmentation is discouraged only because of the fact that it can collide with other augmentations.

According to Javascript Patterns book, its use must be limited.

I personally think it is OK, as long as you use a good naming such as:

String.prototype.mycompany_isMatch = function(s){
   return this.match(s)!==null 
}

This will make it ugly but safe.

6 Comments

Great! (a little bit overkill maybe? :D)
i have not seen those things before. what is ismatch? let me try this. thanks
@magicianIam it will work if you add it to the string prototype - as I have outlined. isMatch is our addition to JavaScript framework, not an intrinsic function
There is no "isMatch" method in String. what Aliostad want to explain is that you can augment javascript object functionality by defining a new function and adding it to it's prototype. Have a look at crockford.com/javascript/inheritance.html fore something more
IMO this is overkill if all he needs it for is a single condition.. this method would be better if you need to make use of this in a lot of places
|
5

there is .indexOf() which will return the position of the string found, or -1 if not found

3 Comments

i've always thought that was only for array.
nope, it works on strings as well! But I'm a bit unclear as to whether you are looking for a substring within a string, or exact (case insenstive?) string comparison..
case sensitive, but i am planning to use tolowercase to ease up my problems. let me try this one. thanks. wonder why google didn't showed me this one.
1
myString.indexOf(myWord) > -1

or, if you want a function:

function hasWord(myString, myWord) {
  return myString.indexOf(myWord) > -1;
}

Comments

0

I know this isn't the exact answer you are looking for but this is always an effective way to do this.

if(var1 == var2){
  //do this
}else{
  //do that
};

2 Comments

according to large number of articles (and my personal opinion too) the ===test is far more reliable (and have transitivity propery) than ==(which is not transitive). Moreover, if he needs a boolean he can just use (var1 === var2 ? true: false)
Yes I understand he wants to return a boolean but what is he going to do with it after, my assumption would be to do something if it is true and something else if it is false. But yes, you are correct with the ===.
0

Actually, .match()can do the trick for you because it returns an array of pattern matching strings if any, null otherwise.

Anyway, if you just want to check if a string contain another string you're more likely to use indexOf() (it will return -1 if no substring is found).

The first solution is a bit overkill for your purpose.

If on the other end you want to check for equality you can use `string1 === string2``

Comments

0

You can just use a compare.

if ("dog" == "cat") {
  DoSomethingIfItIsTrue();
} else {
    DoSomethingIfItIsFalse();
}

1 Comment

Strictly speaking, if you take this approach, you want to use '===', not '=='. I suspect that most Javascript coders would use this rather than augment the String prototype, so it's worth at least getting people in the habit of using '===' for checking equality.
0

If anyone still has doubts ...

It was quoted by another colleague using the indexOf() method. The return of this method is either "-1", if it does not find the String in the Array, or the position of the case it finds.

To use it with the desired return, you would have to perform a check before, such as:

exampleMethod () {
const expression = ['string1', string2] .indexOf (this.exampleObject);
if (expression! = -1) {
 return true;
  } else return false;
}

Basically, you would take advantage of this return.

However, the includes() method can simply be used:

exampleMethode(): boolean {
  return ['string1', 'string2'].includes(this.exampleObject);
}

The return of this method will do what you want in a simple way. Compares the object string with the string array you want to check, and returns a boolean.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.