javascript - 正则表达式只替换一个匹配项
问题描述
我在 JavaScript 中有一个正则表达式代码
const regexns = /[A-Za-z]\:[A-Za-z]/gi;
data = data.replace(regexns, '__NS__');
如果我申请这个 XML
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.zigma.Controller">
我明白了
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmln__NS__x="http://javafx.com/fxml/1"
f__NS__ontroller="com.zigma.Controller">
这意味着我在下一个和上一个字母中丢失了 1 个字母:
如何在:
不丢失这些侧面字母的情况下替换,
正则表达式本身是否有任何选项,或者我们需要执行循环和条件并像那样拆分?
预期输出为
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns__NS__fx="http://javafx.com/fxml/1"
fx__NS__controller="com.zigma.Controller">
解决方案
捕获 the 之前的字母,:
以便将其添加到替换中,并提前查找 the 之后的字母,:
使其不匹配。另请注意,由于您使用的是不区分大小写的标志,因此无需重复[A-Za-z]
,也无需转义冒号:
const data = `<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.zigma.Controller">
`;
console.log(data.replace(/([a-z]):(?=[a-z])/gi, '$1__NS__'));
根据输入的形状,您可以使用单词边界来代替:
const data = `<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.zigma.Controller">
`;
console.log(data.replace(/\b:\b/gi, '__NS__'));
对于更强大的东西,我建议将字符串解析为 XML 文档,然后遍历文档的元素,用:
新属性替换包含模式的属性。