Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  184] [ 6]  / answers: 1 / hits: 27936  / 12 Years ago, tue, may 22, 2012, 12:00:00

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


More From » javascript

 Answers
6

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.


[#85417] Monday, May 21, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pariss

Total Points: 255
Total Questions: 99
Total Answers: 117

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;