我有HTML代码之前和之后的字符串:
name="some_text_0_some_text"
我想用像这样的东西替换0:!NEW_ID!
所以我做了一个简单的正则表达式:
.*name="\w+(\d+)\w+".*
但是我不知道如何专门替换捕获的块。
是否有一种方法替换捕获的结果,如($1)与其他字符串?
结果将是:
name="some_text_!NEW_ID!_some_text"
我有HTML代码之前和之后的字符串:
name="some_text_0_some_text"
我想用像这样的东西替换0:!NEW_ID!
所以我做了一个简单的正则表达式:
.*name="\w+(\d+)\w+".*
但是我不知道如何专门替换捕获的块。
是否有一种方法替换捕获的结果,如($1)与其他字符串?
结果将是:
name="some_text_!NEW_ID!_some_text"
当前回答
对马太的答案稍加改进,可以是一个前瞻,而不是最后一个捕捉组:
.replace(/(\w+)(\d+)(?=\w+)/, "$1!NEW_ID!");
或者你可以在小数点上拆分,并像这样与你的新id连接:
.split(/\d+/).join("!NEW_ID!");
示例/基准在这里:https://codepen.io/jogai/full/oyNXBX
其他回答
要知道,如果需要转换和操作捕获组,可以使用转换器函数作为第二个参数……
API
replace(
regex,
(matched, capture1, capture2, /*...,*/ capture_n, index, input_str) => transformed(/*...*/)
)
replace(
regex: Regex,
transformer: (matched: string, capture1: string, capture2: string, /*...,*/ capture_n: string, index: number, input_str: string) => string
) => string
捕获的数量与您在正则表达式中使用的数量相关。Index和input_str是最后一个。
请参阅下面的示例及其输出,以更好地了解每个示例的含义。
医生参考:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#making_a_generic_replacer
例子:
// To uses example
const propsArgs = args.map((arg) =>
arg.slice(2).replace(/-(.)/g, (matched, captureGroup, index, input) => {
return captureGroup.toUpperCase();
})
);
// To uses example multiple captures groups
const propsArgs = args.map((arg) =>
arg
.slice(2)
.replace(/-(.)(.)/g, (matched, capture1, capture2, index, input) => {
return capture2.toUpperCase();
})
);
// To uses example multiple captures groups args destructuring version
// args[0] matched, args[1] capture 1, ....., args[n] capture n, args[n+1] index, args[n+2] total string to replace.
const propsArgs = args.map((arg) =>
arg.slice(2).replace(/-(.)(.)/g, (...args) => {
return args[2].toUpperCase(); // capture 2
})
);
// example for understanding
const propsArgs = args.map((arg) =>
arg.slice(2).replace(/-(.)/g, (...args) => {
console.log(args); // [ '-f', 'f', 6, 'config-file' ]
return args[1].toUpperCase();
})
);
// multiple capture groups and the args order
/**
* matched string, then all the captures arg after another, then index, then total input string to replace
*/
const propsArgs = args.map((arg) =>
arg
.slice(2)
.replace(
/-(.)(.)(.)/g,
(matched, capture1, capture2, capture3, index, input) => {
// [ '-wat', 'w', 'a', 't', 3, 'log-watch-compilation' ]
return capture1.toUpperCase();
}
)
);
上面的核心示例是将命令行参数转换为javascript的驼峰格式。
转换:
[
'--filename',
'--config-file',
'--env-name',
'--no-swcrc',
'--ignore',
'--only',
'--watch',
'--quiet',
'--source-maps',
'--source-map-target',
'--source-file-name',
'--source-root',
'--out-file',
'--out-dir',
'--copy-files',
'--include-dotfiles',
'--config',
'--sync',
'--log-watch-compilation',
'--extensions'
]
to
[
'filename', 'configFile',
'envName', 'noSwcrc',
'ignore', 'only',
'watch', 'quiet',
'sourceMaps', 'sourceMapTarget',
'sourceFileName', 'sourceRoot',
'outFile', 'outDir',
'copyFiles', 'includeDotfiles',
'config', 'sync',
'logWatchCompilation', 'extensions'
]
一个更简单的选择是只捕获数字并替换它们。
Const name = ' beforeing_text_0_following_text '; Const matcher = /(\d+)/; //替换成你想要的任何东西 const newName = name。替换(匹配器,NEW_STUFF); console.log("完全替换",newName); //使用函数对匹配和替换进行操作 //在这种情况下,使用箭头函数增加它 const incrementedName = name。替换(matcher, (match) => ++match); console.log(“增量”,incrementedName);
资源
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
"some_text_0_some_text".replace(/(?=\w+)\d+(?=\w+)/, '!NEW_ID!')
结果是
some_text_ NEW_ID !_some_text
(?=\w+)\d+(?=\w+)/; const NEW_ID = '! Const STR = 'some_text_0_some_text'; const result = str.replace(regExp, newID); console.log(结果);
JS RegExp中的x(?=y)
仅当x后面有y时匹配x。例如,/Jack(?=Sprat)/只匹配“Jack”后面跟“Sprat”。 /Jack(?=Sprat|Frost)/只有后面跟“Sprat”或“Frost”时才能匹配“Jack”。然而,“Sprat”和“Frost”都不是比赛结果的一部分。
细节
对马太的答案稍加改进,可以是一个前瞻,而不是最后一个捕捉组:
.replace(/(\w+)(\d+)(?=\w+)/, "$1!NEW_ID!");
或者你可以在小数点上拆分,并像这样与你的新id连接:
.split(/\d+/).join("!NEW_ID!");
示例/基准在这里:https://codepen.io/jogai/full/oyNXBX
如果有两个捕获组也是可能的;我还会包括两个破折号,作为额外的左右边界,在数字之前和之后,修改后的表达式将看起来像:
(.*name=".+_)\d+(_[^"]+".*)
Const regex = /(.*name=".+_ ")\d+(_[^"]+".*)/g; Const STR = ' some_data_before name="some_text_0_some_text" and some_data after '; const subst = ' $1!NEW_ID!$2 '; Const result = str.replace(regex, subst); console.log(结果);
如果你想探索/简化/修改这个表达式,它已经被 在右上方的面板上有解释 regex101.com。如果你愿意,你可以 还能在这看吗 链接,如何匹配 对照一些样本输入。
RegEx电路
jex。Im可视化正则表达式: