JS gets the position of the nth occurrence of a specified string in a string

JS gets the position of the nth occurrence of a specified string in a string

Learn about similar methods for getting character positions:

charAt() Gets the character at the specified position in the string

Usage: 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 topic

Get 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:
  • How to swap substring positions in PHP
  • Example of using the IndexOf function to find the position of a string in PowerShell
  • How to determine the position of a string in another string in PHP

<<:  Ubuntu terminal multi-window split screen Terminator

>>:  Summary of online MYSQL synchronization error troubleshooting methods (must read)

Recommend

Nginx configuration location matching rules example explanation

The scope of nginx configuration instructions can...

Vue implements zoom in, zoom out and drag function

This article example shares the specific code of ...

Briefly understand the MYSQL database optimization stage

introduction Have you ever encountered a situatio...

How to Dockerize a Python Django Application

Docker is an open source project that provides an...

How to modify the MySQL character set

1. Check the character set of MySQL show variable...

How to set a dotted border in html

Use CSS styles and HTML tag elements In order to ...

Several common redirection connection example codes in html

Copy code The code is as follows: window.location...

jQuery plugin to implement floating menu

Learn a jQuery plugin every day - floating menu, ...

How to limit the value range of object keys in TypeScript

When we use TypeScript, we want to use the type s...

Steps to install RocketMQ instance on Linux

1. Install JDK 1.1 Check whether the current virt...

Windows platform configuration 5.7 version + MySQL database service

Includes the process of initializing the root use...

Common date comparison and calculation functions in MySQL

Implementation of time comparison in MySql unix_t...