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

Javascript front-end optimization code

Table of contents Optimization of if judgment 1. ...

5 ways to quickly remove the blank space of Inline-Block in HTML

The inline-block property value becomes very usef...

Solution to interface deformation when setting frameset height

Currently I have made a project, the interface is ...

Vue shopping cart case study

Table of contents 1. Shopping cart example 2. Cod...

How to make JavaScript sleep or wait

Table of contents Overview Checking setTimeout() ...

Two ways to open and close the mysql service

Method 1: Use cmd command First, open our DOS win...

Common naming rules for CSS classes and ids

Public name of the page: #wrapper - - The outer e...

Introduction to vim plugin installation under Linux system

Table of contents Install vim plugin manager Add ...

How to add file prefixes in batches in Linux

You need to add "gt_" in front of the f...

Vue implements paging function

This article example shares the specific code of ...

Implementation of running springboot project with Docker

Introduction: The configuration of Docker running...

A detailed introduction to Tomcat directory structure

Open the decompressed directory of tomcat and you...

Detailed explanation of building MySQL master-slave environment with Docker

Preface This article records how I use docker-com...