JS can use to determine whether a string contains another character
String method
1, the indexOf
IndexOf returns the position where the specified string was first found in that character. If it is not found, it returns -1
IndexOf takes two arguments. The first argument is the string to be searched and the second argument is the position to be retrieved
let str = 'abcde';
//For example, search from the third position of str 'a'
console.log(str.indexOf('a',2));// -1
console.log(str.indexOf('a'))// 0
2, lastIndexOf
LastIndexOf takes two arguments, the first is the string to be searched and the second is the position to be retrieved. The default is sting.length-1
let str = 'abcdea';
//For example, search from the third position of str 'a'
console.log(str.lastIndexOf('a',2));// 0
console.log(str.lastIndexOf('a'));// 5
3, includes
The includes() method determines whether a string contains the specified substring and returns true or false
Includes accepts two parameters, the first for the specified string and the second for the lookup location, which defaults to 0
let str = 'abcde';
console.log(str.includes('a'))//true
console.log(str.includes('a',1))//false
4, the match
g: Global search
: Ignoring case
I: The match method retrieving a specified value within a string, or finding a match of one or more regular expressions, returns null if not found (can also be used to query the number of occurrenches of a character in the string)
let str = 'abcdabcda';
console.log(str.match(/a/gi));//['a','a','a']
console.log(str.match(/z/gi));// null
5, the search
The seacrh method is used to retrieve the substring specified in the string, or to retrieve the substring that matches the regular expression, or to return -1 if none
let str = 'abcde';
console.log(str.search('a'));// 0
console.log(str.search(/A/i));//Use regular match to ignore case search Return 0
Regular expression RegExp objects
1. Test method
Retrieves the value specified in the string. Returns true or false.
let str = 'abcdef';
let reg = /A/i;
console.log(reg.test(str));// true
2. Exec method
Retrieves the value specified in the string. Returns the value found and determines its location.
Returns a matching value if there is one in the string, otherwise returns null.
let str = 'abcdef';
console.log(/a/.exec(str))// Return the matching object
console.log(/z/.exec(str))// null
Read More:
- JS to find the last character of the string and remove it
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- [Solved] TypeError: Converting circular structure to JSON – JSON.stringify
- [Go] Can structure/structure pointer be compared with operator == is not defined error
- Json.parse: All Error & How to Solve Them
- Localstorage sets the expiration time.
- [Solved] Failed to load config “prettier“ to extend from.?
- @requestbody: How to Use or Not Use
- TypeScript error TS2345: Argument of type ‘String ‘is not assignable to parameter of type ‘string’
- Vue2.0: How to Use vue3 api to encapsulate Axios
- [Solved] ajax Error: Uncaught SyntaxError: Unexpected end of JSON input
- Use of $watch in Vue (solve rangeerror: maximum call stack size exceeded)
- eslint Error: Delete `␍` [How to Solve]
- JS uses onerror to automatically catch exceptions
- Android realizes the fade in and fade out of animation effect
- vue.config.js build Package UglifyJsPlugin to clear console and print console.log Error: `warnings` is not a supported option
- [Solved] Vue prettier error: error Delete `␍` prettier/prettier
- [Solved] JS Error: Uncaught SyntaxError: Illegal return statement
- Solving routing errors by rewriting Vue push method
- How to use Runtime.getRuntime().Exec()