简体   繁体   中英

Create a regular expression with the tags?

There are three lines on the pages (000) 000-0-000 and +00 (000) 000-00-00 so I wrote a regular expression that searches for them /([\\+]?[0-9]+\\s)?[\\(][0-9]+[\\)]?[\\s][0-9-]+/ , but the third string is enclosed in tags <strong>(0000)</strong><span>00-00-00</span> how to modify the expression to come across and the third row?

function PhoneReplace(){
            var src = $('body').html().replace(/([\+]?[0-9]+\s)?[\(][0-9]+[\)]?[\s][0-9-]+/g, '098 907 23 42');
            $('body').html(src);

        };

tags can not be deleted, I must be replaced

Don't catch that case in your existing regular expression. Instead, modify the string before you apply your phone number expression, ie remove the tags. There are many ways to do so, eg use a DOM parser, regexes might not be the best tool for that. You could do

var results = input.replace(/<[^>]+>/g,"").match(myPhoneNumberExpression);

to remove all tag-like things.

var s = '<strong>(0000)</strong><span>00-00-00</span>';
var n = '', result;
var regex = /(\+?\d+)/g;
while ((result = regex.exec(s))) { 
  n += result[1];
} 
document.writeln(n);

Check this code here .

You can use document.body.innerText or document.body.textContent to get at the textual content (without tags) of the HTML and you can then apply your phone# regular expression.

If the textual content doesn't help, then Strip HTML from Text JavaScript explains several (hacky) ways to remove tags from a string of HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM