0

this ori html code

<td>Name: <b>ozawa</b><br />Country: <b>Japan</b></td>
<td>Name: <b>alexas</b><br />Country: <b>USA</b></td>

my js code

document.body.innerHTML = document.body.innerHTML.replace(new RegExp('<Name: <b>', 'g'),'Name: <a href="http://www.example.com/user/')
                                                 .replace(new RegExp('<\/b><br>Country:', 'g'),'" target="_blank" ><b>???????<\/b><\/a><br>Country:');

output from my js

<td>Name: <a href="http://www.example.com/user/ozawa" target="_blank" ><b>???????</b></a><br />Country: <b>Japan</b></td>
<td>Name: <a href="http://www.example.com/user/alexas" target="_blank" ><b>???????</b></a><br />Country: <b>USA</b></td>

how change ??????? to name ozawa and alexas use js, any idea ?

i want ouput

<td>Name: <a href="http://www.example.com/user/ozawa" target="_blank" ><b>ozawa</b></a><br />Country: <b>Japan</b></td>
<td>Name: <a href="http://www.example.com/user/alexas" target="_blank" ><b>alexas</b></a><br />Country: <b>USA</b></td>

Thanks for everybody who can help me :D

5
  • output from my js is that output from your js code? Commented Nov 23, 2013 at 12:15
  • and why you place ??????? there? Commented Nov 23, 2013 at 12:16
  • document.body.innerHTML = document.body.innerHTML.replace(new RegExp('<Name: <b>', 'g'),'Name: <a href="http://www.example.com/user/') .replace(new RegExp('<\/b><br>Country:', 'g'),'" target="_blank" ><b>???????<\/b><\/a><br>Country:'); replace by document.body.innerHTML = document.body.innerHTML.replace(new RegExp('<Name: <b>', 'g'),'Name: <a href="http://www.example.com/user/') .replace(new RegExp('<\/b><br>Country:', 'g'),'" target="_blank" ><b>ozawa<\/b><\/a><br>Country:'); Commented Nov 23, 2013 at 12:20
  • now i'm edit again to 1st code, code error after change. now use 2 lines. Commented Nov 23, 2013 at 12:33
  • what error you are getting/ Commented Nov 23, 2013 at 12:33

1 Answer 1

3

This would be a million times easier done with PHP. Anyway,

document.body.innerHTML = document.body.innerHTML
.replace(new RegExp('<td>Name: <b>(.*?)<\/b>', 'g'),
'<td>Name: <a href="http://www.example.com/user/$1" target="_blank"><b>$1</b></a>');

You should seriosuly consider removing those <b> tags and using css instead.

Sign up to request clarification or add additional context in comments.

2 Comments

@jamesbond I had a slight mistake in the regex, ? was placed outside (.*). I edited the answer and it is correct now. Good luck! :)
Gud one +1 from my side

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.