Jquery 查找没有包装元素的电话号码

Jquery 查找没有包装元素的电话号码,jquery,regex,Jquery,Regex,我用正则表达式在文本块中查找所有电话号码。现在我想忽略所有已经包装在中的数字。我按照BurnsBA的建议遍历树并忽略a节点,从而解决了我的问题 // First I create a variable with the desired regex // While this won't catch all number formats, // it works decently for my use case. var telRegex = new RegExp(/(\+[1-9][0-9]*

我用正则表达式在文本块中查找所有电话号码。现在我想忽略所有已经包装在
中的数字。我按照BurnsBA的建议遍历树并忽略a节点,从而解决了我的问题

// First I create a variable with the desired regex
// While this won't catch all number formats, 
// it works decently for my use case.
var telRegex = new RegExp(/(\+[1-9][0-9]*(\([0-9]*\)|-[0-9]*-))?[0]?[1-9][0-9\- ]*\d/, 'g');

// We get all <a> tags with a tel:-href
var $telLinks = $('a[href^="tel:"]');
var linkArray = [];
var content;

// Next we clone all <a href=tel:> tags to an array
$telLinks.each(function(index, link) {

    linkArray.push($(link).clone());

});

// Then we replace these <a> tags with placeholders.
$('a[href^="tel:"]').replaceWith('<a href="#" class="tmp-tel"></>');

// Now all manually placed <a href=tel:> tags won't 
// bother us when we run our regex
content = $('body').html();

// We wrap all identified phone numbers with an <a href=tel:> tag
// and replace the content
content = content.replace(telRegex, '<a href="tel:$&">$&</a>');
$('body').html(content);

// Then we find all of our placeholders and replace then with
// our saved elements
$('body').find('a.tmp-tel').each(function(index, value) {

    value.outerHTML = linkArray[index][0].outerHTML;

});
//首先,我使用所需的正则表达式创建一个变量
//虽然这不能捕获所有的数字格式,
//它在我的用例中正常工作。
var telRegex=newregexp(/(\+[1-9][0-9]*(\([0-9]*\)-[0-9]*-)?[0]?[1-9][0-9\-]*\d/,'g');
//我们得到了一切;
$('body').html(内容);
//然后我们找到所有的占位符并替换为
//我们保存的元素
$('body')。查找('a.tmp-tel')。每个(函数(索引,值){
value.outerHTML=linkArray[index][0];
});
确实有更好的方法可以做到这一点,但这个解决方案解决了我的问题

下面可以查看完整的示例:


使用html库处理文档。遍历树并忽略节点。对于常见的美国编号,如
123.456.7890
(123)-456-7890
,此操作也会失败。您还应该包括组的上限,因为
11111111111111111111111111
是一个有效的数字。到底应该和不应该匹配什么?它是否应该在
属性中匹配?电话号码应该是独立的还是与其他文字混在一起?(如描述)。正则表达式是一种非常精确的语言。我们需要的正是您想要/不想要的,或者我们无法提供足够的答案。您正在尝试匹配哪些电话号码格式?你知道可能存在数百种电话号码格式,对吗?对不起,我应该说得更清楚些。我试图匹配最常见的斯堪的纳维亚电话号码,但在第一次迭代中,它并不一定是完美的。我正在编写一个函数来自动将电话号码包装在用户忘记这样做的a标签中。我按照BurnsBA的建议设法解决了我的问题,我很快就会给出答案。
// First I create a variable with the desired regex
// While this won't catch all number formats, 
// it works decently for my use case.
var telRegex = new RegExp(/(\+[1-9][0-9]*(\([0-9]*\)|-[0-9]*-))?[0]?[1-9][0-9\- ]*\d/, 'g');

// We get all <a> tags with a tel:-href
var $telLinks = $('a[href^="tel:"]');
var linkArray = [];
var content;

// Next we clone all <a href=tel:> tags to an array
$telLinks.each(function(index, link) {

    linkArray.push($(link).clone());

});

// Then we replace these <a> tags with placeholders.
$('a[href^="tel:"]').replaceWith('<a href="#" class="tmp-tel"></>');

// Now all manually placed <a href=tel:> tags won't 
// bother us when we run our regex
content = $('body').html();

// We wrap all identified phone numbers with an <a href=tel:> tag
// and replace the content
content = content.replace(telRegex, '<a href="tel:$&">$&</a>');
$('body').html(content);

// Then we find all of our placeholders and replace then with
// our saved elements
$('body').find('a.tmp-tel').each(function(index, value) {

    value.outerHTML = linkArray[index][0].outerHTML;

});