gigala.blogg.se

Javascript string contains substring
Javascript string contains substring









This means that you can compare the value returned by the indexOf() method to see if it is equal to -1. If the substring or word is not found, it returns -1. This method returns the index of the first occurrence of the specified substring inside the calling String object. The fastest way to check if a string contains a particular word or substring is with the help of String.indexOf() method. Using String.indexOf() to Check if String Contains Substring Using String.match() to Check if String Contains Substring.Using arch() to Check if String Contains Substring.Using String.includes() to Check if String Contains Substring.Using String.indexOf() to Check if String Contains Substring.Str.toLowerCase().includes( 'Snow'.toLowerCase()) // false Str.toLowerCase().indexOf( 'Stark'.toLowerCase()) != -1 // true Str.toLowerCase().includes( 'Stark'.toLowerCase()) // true Str.match( /Snow/i) // false // You can also convert both the string and the search string to lower case. The most concise way to check substrings ignoring case is using // `String#match()` and a case-insensitive regular expression (the 'i') To do case insensitive search, you can use regular expressions and the String#match() function, or you can convert both the string and substring to lower case using the String#toLowerCase() function.

javascript string contains substring

Neither function supports regular expressions.

javascript string contains substring

Case Insensitive Searchīoth String#includes() and String#indexOf() are case sensitive. The includes() function's syntax is only marginally more concise than indexOf(). In general, if you have any doubt about whether code will run in an environment that supports includes(), you should use indexOf(). If you need to support Internet Explorer, you should instead use the String#indexOf() method, which has been a part of JavaScript since ES1 in 1997. You can also use String#includes() in Node.js >= 4.0.0.Ĭompatibility table from Mozilla Developer Network You can use String#includes() in all modern browsers except Internet Explorer.

javascript string contains substring

The more modern way is the String#includes() function. There's two common ways to check whether a string contains a substring in JavaScript.











Javascript string contains substring