JavaScript String Object Methods

JavaScript String Object Methods

Methods of String Object

Method 1: indexOf() (recommended)

var str = "123";
console.log(str.indexOf("3") != -1 ); // true

The indexOf() method returns the position of the first occurrence of a specified string value within a string. If the string value to be retrieved does not appear, the method returns -1.

Method 2: search()

var str = "123";
console.log(str.search("3") != -1 ); // true

The search() method is used to search for a specified substring in a string, or to search for a substring that matches a regular expression. If no matching substring is found, -1 is returned.

Method 3: match()

var str = "123";
var reg = RegExp(/3/);
if(str.match(reg)){
    // Include}

The match() method retrieves a specified value within a string, or finds matches for one or more regular expressions.

RegExp Object Methods

Method 4: test()

var str = "123";
var reg = RegExp(/3/);
console.log(reg.test(str)); // true

The test() method is used to retrieve the value specified in a string. Returns true or false.

Method 5: exec()

var str = "123";
var reg = RegExp(/3/);
if (reg.exec(str)) {
    // Include}

The exec() method is used to retrieve matches of a regular expression within a string. Returns an array containing the matching results. If no match is found, the return value is null.

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Basic usage examples of JavaScript string object (string)
  • Introduction to methods of String object in JavaScript
  • Summary of common methods of array objects and string objects in javascript
  • Detailed explanation of Javascript String object
  • Summary of common operations of JavaScript String object
  • Detailed explanation of common methods of JavaScript String object
  • Summary of Common Methods of JavaScript String Object

<<:  Use IISMonitor to monitor web pages and automatically restart IIS

>>:  Rounding operation of datetime field in MySQL

Recommend

Calendar effect based on jQuery

This article example shares the specific code of ...

About the location of the H1 tag in XHTML

There has been a lot of discussion about H1 recent...

MySQL efficient query left join and group by (plus index)

mysql efficient query MySQL sacrifices group by t...

jQuery realizes the shuttle box effect

This article example shares the specific code of ...

HTML embedded in WMP compatible with Chrome and IE detailed introduction

In fact, there are many corresponding writing met...

The magic of tbody tag speeds up the display of table content

You must have saved other people’s web pages and l...

How to install grafana and add influxdb monitoring under Linux

Install grafana. The official website provides an...

Essential conditional query statements for MySQL database

Table of contents 1. Basic grammar 2. Filter by c...

...

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

HTML tags: sub tag and sup tag

Today I will introduce two HTML tags that I don’t...

40 CSS/JS style and functional technical processing

1- Styling dropdown select boxes - Modify the dro...

JavaScript to implement the function of changing avatar

This article shares the specific code of JavaScri...