![]() ![]() ![]() The code example below shows how you can use the indexOf method to check if a string contains a substring in ECMAScript 5 or older. 3 Answers Sorted by: 15619 ECMAScript 6 introduced : const string 'foo' const substring 'oo' console. } Option 2: Use the indexOf Method (ECMAScript 5 or older) run this code if People is present in the str constant const str = 'We the People of the United States, in Order to form a more perfect Union.' Ĭonsole.log(str.includes('We the')) // trueĬonsole.log(str.includes('Order')) // trueĬonsole.log(str.includes('notpresent')) // falseĬonsole.log(str.includes('WE THE')) // false The code snippet below shows you how to use the includes method to check if a string contains a substring in ECMAScript 6. You can check if a JavaScript string contains a character or phrase using the includes() method, indexOf(), or a regular expression. Option 1: Use the includes Method (ECMAScript 6) var actualstring 'In ES5 use IndexOf method', var substringToCheck 'IndexOf' var isContainsactualstring.indexOf (substringToCheck) -1 //isContains true. Go through the following code example to understand it further. One of the ways is done using the includes method for JavaScript 6 (ECMAScript 6) and the other is with the indexOf for JavaScript 5 (ECMAScript 5) or older. In ES5 and before versions of Javascript we can use indexOf method to check if a string contains a substring or not. There are two simple ways to check if a string contains a substring in JavaScript.
0 Comments
Leave a Reply. |