2012-01-19 6 views
0

I持ってこの正規表現/ javascriptのコード:

toReturn = toReturn.replace(/(\[url=)(.+)(\])(.+)(\[\/url\])/g, '<a target=\"_blank\" href="\$2">\$4</a>'); 

しかし、問題である、ユーザ入力の場合:

[url=example]example[/url] 
some_other_text 
[url=example2]example2[/url] 

それがします最初の[url =]を最後の[/ url]にリンクしてください。間違ったURLを1つだけ作成する!!!!!

どうすればこの問題を解決できますか?

+0

ユーザーが '[url = javascript:location = http://evil.org] [/ url]'または '[url =">

+0

And how would I stop that Mike Samuel? –

+0

by white-listing URLs by protocol, and properly encoding both the HTML attribute value and the HTML text node using entity escapes. –

答えて

2

(.+)の代わりに(.+?)を使用すると、定量化が遅延します。

+0

完璧に動作します、ありがとう! –