0

My source file here contains HTML code where I want to change the phone number to be clickable in my app. I am looking a regular expression to convert string >number< into ><a href="tel:number">number</a><

Simple /\d/ regex wont work because there is html code such as style="color: #333333;

Please help. Thanks.

Iyas
  • 520
  • 1
  • 11
  • 40
  • 2
    You should read [this post](http://blog.codinghorror.com/parsing-html-the-cthulhu-way/). – DontVoteMeDown Dec 03 '14 at 17:58
  • 1
    @DontVoteMeDown Perhaps you should also refer to this one: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Ismael Miguel Dec 03 '14 at 18:30

2 Answers2

2

This is the shortest answer I can come up with:

html=html.replace(/>(\d+)<\//g,'><a href="tel:$1">$1</a></');

This is untested code and is not guaranteed to work (for now).

But should suffice for now.

Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42
  • Thanks! With minor edit `html=html.replace(/>(\d+)<\//g,'>$1');` THIS IS WHY I LOVE STACKOVERFLOW!!!! – Iyas Dec 03 '14 at 18:19
  • @lyas That will work too, but I left the `">` bit on purpose. If you check the code, you will see the phone numbers are wrapped inside ` your number `. It's more likely to find `>` than `">`. I don't disagree with your suggestion, I just simply prefer on this way to be a tiny bit safer. – Ismael Miguel Dec 03 '14 at 18:23
  • Yes, but it will rules out

    number

    which are exist in the code. Thanks a lot! You save my time. May God bless you.
    – Iyas Dec 03 '14 at 18:25
  • @lyas Didn't noticed those. I overlooked the code and only found that pattern. I will fix it now. I'm glad I could help you. This was actually an easy one. – Ismael Miguel Dec 03 '14 at 18:27
  • 1
    @lyas You don't need to learn from the books. You should practice. This is pretty much like math. https://regex101.com/ is a great website to build and test your regex. It explains what it does step by step with a description for each. One good resource is this one: http://stackoverflow.com/questions/4736/learning-regular-expressions (which also supports my view). If you really insist in books, you can check http://www.regular-expressions.info/books.html (**WARNING: I DIDN'T TRIED TO BUY** ANY **OF THOSE BOOKS! I GIVE NO WARRANTY AT ALL REGARDING THE SELLER AND THE CONTENT!**) – Ismael Miguel Dec 03 '14 at 20:12
1

I think you're looking for something like this regular expression:

(.*>)([0-9]+)(<.*)

And then you'd replace it with this:

$1<a href="tel:$2">$2</a>$3
BrianV
  • 961
  • 8
  • 9
  • 1
    This code will be a hog when used in the example code. I will be surprised if it doesn't make the browser run out of memory. And you can safely replace `[0-9]+` with `\d+`. +1 for the effort. – Ismael Miguel Dec 03 '14 at 18:18
  • I missed the fact that the question were looking for a js-specific solution. This was meant to convey the regex in conceptual terms. Ismael, your solution is exactly what I would have recommended in terms of a js replace. And of course \d works just as well for digits... I sometimes prefer the more verbose syntax because I find it more readable. Too many escape characters makes my head explode. :) – BrianV Dec 03 '14 at 20:20
  • I agree with your view. In this case, it is only 1 escape and the readability won't be hurt. Since the OP is trying to learn from this, your example teaches a little better. Regarding about being javascript or not, I still think it would be a hog but your solution is pretty close to mine (I can't see any other "correct" answer). Don't delete this answer, just add a note explaining that you were explaining in a general context. (I already upvoted yours, can't do more than that.) – Ismael Miguel Dec 03 '14 at 20:24