0

I am trying to write simple function in JAVASCRIPT which can customize my html code with Tampermonkey installed on my chrome web browser. My input html is an address:

'John OConnor<br><br>
Shop St 7<br>
Dublin<br>
12<br>
IE'

This address is saved in my variable named "address". I used some code to customize it:

address = address.replace(/<br><br>/gi, '<br>'); //remove duplicated <br> tag
address = address.replace(/St/gi, 'Street'); //change st to street
address = address.replace(/IE/gi, 'Ireland'); //change IE to Ireland

now my html output looks like this:

'John OConnor<br>
Shop Street 7<br>
Dublin<br>
12<br>
Ireland'

my question now is how to write a code to move that "12" to get "Dublin 12"? I was trying this code:

address = address.replace(/<br>\d\d<br>/gi, '\d\d<br>')

\d = return any digit

but it returns: "Dublin \d\d" and I want "Dublin 12"

hope you understand. any idea?

4
  • 1
    If someone provides you with an address then assume that they know what they are talking about and don't change it. What happens when the name is Brien Fields and it becomes BrIrelandn FIrelandlds or if the name is Steve Stobart and it becomes Streeteve Streetobart? Commented Feb 7, 2014 at 12:03
  • I want to convert address to "more nice look". To avoid your example I just can change replace function to this .replace(/<br>IE<br>/gi, '<br>Ireland<br>') Commented Feb 7, 2014 at 12:09
  • If you are using the address to deliver goods/services to the user and your code mangles the address so that the postman cannot deliver it or delivers it to the wrong place then you will lose business. Better to assume that the user is half-way competent when it comes to knowing their own address (and if they aren't then it is their fault not yours) rather than trying to do something "clever" and second guessing where someone else lives. Commented Feb 7, 2014 at 12:13
  • Thank you for your opinion. I am careful when I customize address to don't change the content, just to show this in better way that postman can see it better and it takes less lines. Example, input: dublin<br>co dublin<br>dublin d12<br>default<br>ireland output: Dublin 12<br>Ireland Commented Feb 7, 2014 at 12:17

2 Answers 2

1
address = address.replace(/<br>(\d\d)<br>/gi, '$1<br>')
Sign up to request clarification or add additional context in comments.

2 Comments

\d matchs any digit, what if I need to match any number like "023" or "132545" ??
If you want to match a flexible number of digits you can either use "\d+" (1 or more digits) instead of "\d\d". Otherwise if you only want to replace 2 or more digits use "\d{2,}". A great tool for learning regex is regexr: regexr.com
1

An alternate way to capture the digits

This will match numbers ranging from 3 digit to 9 digit.

address = address.replace(/<br>\d{3,9}<br>/gi, '$1<br>')

solution to the question you commented

address = address.replace(/<br>(023|132545)<br>/gi, '$1<br>')

1 Comment

[[:digit:]] is also used to capture digits.Limits can be specified like {2,} or simply adding plus symbol + which means 0 or more times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.