Home > Web Front-end > JS Tutorial > body text

How to replace intermediate content in JavaScript

醉折花枝作酒筹
Release: 2021-06-10 11:00:55
Original
2818 people have browsed it

In JavaScript, first create a method that can replace the intermediate content; then use the substr method to extract text that does not need to be replaced. The syntax format is "String object.substr (starting position, number of characters)"; finally Just replace the content that needs to be replaced.

How to replace intermediate content in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

If you want to replace the strings such as ID number or mobile phone number in the middle of the string

 function replacepos(text,start,stop,replacetext){
      let mystr = text.substr(0,start-1)+replacetext+text.substr(stop+1);
      return mystr;
    },
 replacepos('测试文字csdn', 2, 4, "**")  // 测试**csdn
Copy after login

substr() method can extract the specified number starting from the start subscript in the string character.

Syntax

stringObject.substr(start,length)
Copy after login

How to replace intermediate content in JavaScript

Return value

A new string containing start from stringObject (including the point pointed to by start characters) starting at length characters. If length is not specified, the returned string contains characters from start to the end of stringObject.

Tips and Notes

Note: The parameters of substr() specify the starting position and length of the substring, so it can be used instead of substring() and slice().

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to replace intermediate content in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!