JavaScript由指数编号替换()正则表达式

时间:2022-05-11 10:24:49

I have the following string and regex:

我有以下字符串和正则表达式:

var string = "Dear [to name], [your name] has decided to share this [link]"; 
var patt = /\[+[A-Za-z0-9]+\]/;

I want to be able to change each of the bracketed variables with dynamic input. How would I use match() or replace() to target the 1st, 2nd and 3rd occurrences of this regex?

我希望能够使用动态输入更改每个括号中的变量。我如何使用match()或replace()来定位此正则表达式的第1,第2和第3次出现?

EDIT: At the moment if I do something like document.write(body.match(patt)); it will match only the last one [link]

编辑:目前,如果我做像document.write(body.match(patt));它只匹配最后一个[链接]

EDIT: The entire string is take from the value of a text box. The values for each of the brackets are taken from other text inputs and need to be inserted into the string before the text is put back into the text box.

编辑:整个字符串取自文本框的值。每个括号的值都取自其他文本输入,需要在将文本放回文本框之前插入到字符串中。

2 个解决方案

#1


10  

Use a function as the second argument to the replace method:

使用函数作为replace方法的第二个参数:

var replacement = { "to name": "Joe", "your name": "Fred", "link": "foo" };

string = string.replace(/\[([^\]]+)\]/g, function (_, group) {
    return replacement[group];
});

Oh, and the reason your pattern is only matching the [link] text is because it allows only alphanumeric characters between brackets, not spaces.

哦,你的模式只匹配[link]文本的原因是因为它只允许括号之间的字母数字字符,而不是空格。

EDIT: If the content of the brackets is unimportant, and you just want to replace them in order, use an array instead of a hash:

编辑:如果括号的内容不重要,并且您只想按顺序替换它们,请使用数组而不是哈希:

var replacement = [ "Joe", "Fred", "foo" ];
var index = 0;
string = string.replace(/\[[^\]]+\]/g, function () {
    return replacement[index++];
});

#2


3  

Assuming those bracketed items are always the same, you don't have to use regex at all.

假设这些括号中的项目始终相同,则根本不必使用正则表达式。

var string = "Dear [to name], [your name] has decided to share this [link]"; 
var name = 'Bob';
var your_name = 'Jacob';
var link = 'http://google.com';

string = string.replace( '[to name]', name ).replace( '[your name]', your_name ).replace( '[link]', link )

alert( string )​

#1


10  

Use a function as the second argument to the replace method:

使用函数作为replace方法的第二个参数:

var replacement = { "to name": "Joe", "your name": "Fred", "link": "foo" };

string = string.replace(/\[([^\]]+)\]/g, function (_, group) {
    return replacement[group];
});

Oh, and the reason your pattern is only matching the [link] text is because it allows only alphanumeric characters between brackets, not spaces.

哦,你的模式只匹配[link]文本的原因是因为它只允许括号之间的字母数字字符,而不是空格。

EDIT: If the content of the brackets is unimportant, and you just want to replace them in order, use an array instead of a hash:

编辑:如果括号的内容不重要,并且您只想按顺序替换它们,请使用数组而不是哈希:

var replacement = [ "Joe", "Fred", "foo" ];
var index = 0;
string = string.replace(/\[[^\]]+\]/g, function () {
    return replacement[index++];
});

#2


3  

Assuming those bracketed items are always the same, you don't have to use regex at all.

假设这些括号中的项目始终相同,则根本不必使用正则表达式。

var string = "Dear [to name], [your name] has decided to share this [link]"; 
var name = 'Bob';
var your_name = 'Jacob';
var link = 'http://google.com';

string = string.replace( '[to name]', name ).replace( '[your name]', your_name ).replace( '[link]', link )

alert( string )​