2012-01-19 2 views
0

I가이 정규식/자바 스크립트 코드 :

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 만들기 !!!!!

어떻게 해결할 수 있습니까?

+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

완벽하게 작동합니다. 감사합니다! –