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

How to use anti-shake and throttling in Vue

Table of contents Preface concept Stabilization d...

Solution to the routing highlighting problem of Vue components

Preface Before, I used cache to highlight the rou...

Detailed explanation of MySQL 8's new feature ROLE

What problems does MySQL ROLE solve? If you are a...

CSS3 category menu effect

The CSS3 category menu effects are as follows: HT...

HTML+CSS3 code to realize the animation effect of the solar system planets

Make an animation of the eight planets in the sol...

ul list tag design web page multi-column layout

I suddenly thought of this method when I was writi...

Tutorial on installing rabbitmq using yum on centos8

Enter the /etc/yum.repos.d/ folder Create rabbitm...

Docker container introduction

1. Overview 1.1 Basic concepts: Docker is an open...

HTML validate HTML validation

HTML validate refers to HTML validation. It is the...

Three ways to achieve text flashing effect in CSS3 Example code

1. Change the transparency to achieve the gradual...

How to deploy ElasticSearch in Docker

1. What is ElasticSearch? Elasticsearch is also d...

The best 9 foreign free picture material websites

It is difficult to find good image material websi...

How to automatically deploy Linux system using PXE

Table of contents Background Configuring DHCP Edi...