如何在函数本身的 JS replace() 函数中重用正则表达式的匹配值?
How to reuse matched value of regex in JS replace() function in the function itself?
假设我有一个字符串,我想在某个字符串中进行匹配。为了找到它,我使用正则表达式,然后替换它。但是,我如何将它替换为匹配本身但已编辑?
我很难使用 match() 和 matchAll(),因为输入文本不一定是字符串,而且匹配函数的输出在我使用的上下文中根本没有意义它在。他们 return 作为 RegExpStringIterator(s).
目的:
正在尝试制作 google 编辑页面内容的扩展程序。
例如
regex = /123/g
let text = "XYZ string is some string is a string, but 123 is what I want to change."
let edit = text.replace(regex, numGen(***not sure what to pass here***))
console.log(edit)
function numGen(match) {
//Here I want to get the match and add the string "4" to it
}
"""
Expected outcome of "console.log(edit)":
XYZ string is some string is a string, but ***1234*** is what I want to change.
"""
你是这个意思吗?检查内联评论
UPD 匹配时调用外部函数
// External function to call with each match
const numGen = match => {
// Here you get the match as argument from replace function
// Then add the string "4" to it
match += "4";
// Then return final result back to replace
return match;
}
// Replace method part
const regex = /string/g;
let counter = 0;
let text = "XYZ string is some string is a string.";
// Just set your external function as replace parameter.
// Matched string will be passed to it as arguments
let edit = text.replace(regex, numGen);
// Result
console.log(edit);
根据您的示例,如果您只需要将 string/s (例如文本)附加到匹配结果,您可以简单地使用 replace():
const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, '***$&***');
// $& means the whole matched string
// result: XYZ string is some string is a string, but ***123*** is what I want to change.
如果你需要运行一些代码来改变匹配结果,那么你可以使用replacerFunction
:
const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, function(m) {
// m here means the whole matched string
// for example, let's double the number
return m * 2;
// resulat: XYZ string is some string is a string, but 246 is what I want to change.
});
同上但使用箭头函数:
const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, m => {
// m here means the whole matched string
// for example, let's double the number
return m * 2;
// result: XYZ string is some string is a string, but 246 is what I want to change.
});
注:对于单次操作,上面也可以写成:
const edit = text.replace(regex, m => m * 2);
假设我有一个字符串,我想在某个字符串中进行匹配。为了找到它,我使用正则表达式,然后替换它。但是,我如何将它替换为匹配本身但已编辑?
我很难使用 match() 和 matchAll(),因为输入文本不一定是字符串,而且匹配函数的输出在我使用的上下文中根本没有意义它在。他们 return 作为 RegExpStringIterator(s).
目的:
正在尝试制作 google 编辑页面内容的扩展程序。
例如
regex = /123/g
let text = "XYZ string is some string is a string, but 123 is what I want to change."
let edit = text.replace(regex, numGen(***not sure what to pass here***))
console.log(edit)
function numGen(match) {
//Here I want to get the match and add the string "4" to it
}
"""
Expected outcome of "console.log(edit)":
XYZ string is some string is a string, but ***1234*** is what I want to change.
"""
你是这个意思吗?检查内联评论
UPD 匹配时调用外部函数
// External function to call with each match
const numGen = match => {
// Here you get the match as argument from replace function
// Then add the string "4" to it
match += "4";
// Then return final result back to replace
return match;
}
// Replace method part
const regex = /string/g;
let counter = 0;
let text = "XYZ string is some string is a string.";
// Just set your external function as replace parameter.
// Matched string will be passed to it as arguments
let edit = text.replace(regex, numGen);
// Result
console.log(edit);
根据您的示例,如果您只需要将 string/s (例如文本)附加到匹配结果,您可以简单地使用 replace():
const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, '***$&***');
// $& means the whole matched string
// result: XYZ string is some string is a string, but ***123*** is what I want to change.
如果你需要运行一些代码来改变匹配结果,那么你可以使用replacerFunction
:
const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, function(m) {
// m here means the whole matched string
// for example, let's double the number
return m * 2;
// resulat: XYZ string is some string is a string, but 246 is what I want to change.
});
同上但使用箭头函数:
const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, m => {
// m here means the whole matched string
// for example, let's double the number
return m * 2;
// result: XYZ string is some string is a string, but 246 is what I want to change.
});
注:对于单次操作,上面也可以写成:
const edit = text.replace(regex, m => m * 2);