js replaces a certain character in the string, and js modifies the specified character in the string

js replaces a certain character in the string, and js modifies the specified character in the string

        When using elementui, you often encounter a multi-level tree structure, but the backend often gives inconsistent fields to each level of the frontend. If you write a traversal loop to change it to be consistent, it is troublesome. It is better to directly transfer the array sent by the background. It is a string, and then replace the inconsistent fields in the string with consistent ones, and the method of how to replace the specified characters that need to be replaced in the string is delegated.

The case is as follows

1. Replace all characters that are a in the string with e

let str = 'abcabcabc';
str = str.replace(/a/g, 'e');
console.log(str);
// 打印结果: ebcebcebc

2. Replace the first character of a in the string with e

let str = 'abcabcabc';
str = str.replace(/a/, 'e');
console.log(str);
// 打印结果: ebcabcabc

Onlooker address:

js replaces a certain character in a string, and js modifies a specified character in a string     . When elementui is usually used, a multi-level tree structure is often encountered, but the back-end often gives the front-end the inconsistency of each level of the field. If you write a traversal loop, it will It is troublesome to change it to be consistent. It is better to directly convert the array sent in the background into a string, and then replace the inconsistent fields in the string with consistent ones. How to replace characters..., shared blog http:// sharedbk .com/post/235.html

Guess you like

Origin blog.csdn.net/tianpeng1996/article/details/121104537