2016-10-31 2 views
2

Here is what I've tried so far :URL (문자열)에 언어를 추가/대체하려면 어떻게해야합니까?

/(?:\/(en(\?|$)|fa(\?|$)))|\?/ 

입력 :

http://localhost:8000/search/fa?q=sth 
http://localhost:8000/search?q=sth 
http://localhost:8000/search 
http://localhost:8000/search/fa 

예상 출력 :

http://localhost:8000/search/en?q=sth 
http://localhost:8000/search/en?q=sth 
http://localhost:8000/search/en 
http://localhost:8000/search/en 

알다시피, ?과 일치해야하지만 선택하지 않아야합니다. 또한 세 번째 URL 끝에 /en을 추가해야합니다.

내 패턴을 수정하려면 어떻게해야합니까? 이 여기

를 작동 할 수

답변

1
^.*?\/(\w{2,})(?:\?(.*?)|)$ 

이 정규식 간단한 구현입니다. 당신이 당신의 대답에 약간의 설명을 추가하고 나 같은 초보자에게 더 이해할 수 있도록 경우

DEMO

+1

그것은 훨씬 더 좋을 것이다. – Alex

관련 문제