gpt4 book ai didi

javascript - 用于替换特定电话号码的正则表达式

转载 作者:行者123 更新时间:2023-11-30 05:30:57 26 4
gpt4 key购买 nike

所以我正在研究可以更改网站上特定电话号码的正则表达式。我正在慢慢到达那里,但我不知道如何捕捉以下数字:(085)-4017877(085)4017877。

regexr.com 之类的工具告诉我,正则表达式在此处捕获了两个数字:085-4017877但在我目前的设置中,它没有捕捉到第一个数字。关于为什么会这样有什么想法吗?

当前正则表达式:\85[-.\s]?4017877\g

故意忽略数字前面的零。

它应该捕获什么:

085-4017877085-4017877(085)-4017877(085)4017877085 4017877+31854017877

测试: http://regexr.com/39v0b

       //step through dom
function recurseDOM(scope, newText, patt)
{
var i = 0, nodes, node;
if(scope.childNodes)
{
nodes = scope.childNodes;
for(i;i<nodes.length;i++)
{
node = nodes[i];
if(node.nodeType === 3)
{
//is a text node
checkTextNode(node, newText, patt);
}
if(node.childNodes)
{
//loop through child nodes if child nodes are found
recurseDOM(node, newText, patt);
}
node = null;
}
nodes = null;
}
}

//Find and replace
function checkTextNode(node, newText, patt)
{
var text = node.data;
var test = patt.test(text);

if(test)
{
//match found, replace node's textual data with specified string
node.data = text.replace(patt, newText);
newText = null;
text = null;
}
test = null;
}

我目前用来替换号码的代码

最佳答案

你已经很接近了,这个应该能捕捉到你所有的情况:

/85\)?[- ]?4017877/g

它只是在破折号/空格字符类之前添加一个可选的括号。

关于javascript - 用于替换特定电话号码的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com