Learn about similar methods for getting character positions: charAt() Gets the character at the specified position in the stringUsage: strObj is a string object, index is the specified position (the position starts at 0) strObj.charAt(index) The indexOf() method returns the index of the first occurrence of a specified string value in a string.Usage: stringObject is a string object, searchvalue is the specified string value, fromindex (optional) specifies the position to start matching the string value. If not, it means starting from position 0. stringObject.indexOf(searchvalue,fromindex) For example: var str = 'helloworld'; var num = str.indexOf('o'); // returns 4 Main topicGet the position of the nth occurrence of a string value in the specified string Like the example above, helloword, I want to get the position where the second o appears js code: parameter (string, string value to be searched, the string value to be searched function find(str,cha,num){ var x=str.indexOf(cha); for(var i=0;i<num;i++){ x=str.indexOf(cha,x+1); } return x; } Quote the method: ar str="Hello World!" document.write(find(str,'o',1));//Return 7 The basic usage is like this. For a string with many identical characters, just replace the corresponding 2 with the n value you want to find. For example: Get the position of the nth '/' in the current page URL Call the above method directly ar str=document.URL; //Get the complete path information of the current page document.write(find(str,'/',n)); This is the end of this article about how to get the nth occurrence of a specified string in a string with JS. For more information about how to get the nth occurrence of a specified string in a string with JS, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Ubuntu terminal multi-window split screen Terminator
>>: Summary of online MYSQL synchronization error troubleshooting methods (must read)
The scope of nginx configuration instructions can...
This article example shares the specific code of ...
Logpoint-based replication 1. Create a dedicated ...
introduction Have you ever encountered a situatio...
Docker is an open source project that provides an...
1. Check the character set of MySQL show variable...
Use CSS styles and HTML tag elements In order to ...
Copy code The code is as follows: window.location...
Learn a jQuery plugin every day - floating menu, ...
When we use TypeScript, we want to use the type s...
1. Install JDK 1.1 Check whether the current virt...
Includes the process of initializing the root use...
Because the distribution package of MySQL Communi...
1. Python installation 1. Create a folder. mkdir ...
Implementation of time comparison in MySql unix_t...