regex - RegExp replace after -
i have link templates , need replace substrings inside of links. link templates:
- "/all_news"
- "/all_news/"
- "/all_news/page1"
- "/all_news/page1/"
all of these templates mean same thing - first page of news page without filtering.
so need to:
1st template - insert "/pagex"
2nd template - insert "pagex"
3rd , 4th templates - replace page number
is possible 1 regexp?
if yes, please me.
if no, have 2nd question: maybe possible replace after "/all_news" on "/pagex"?
i mean next logic:
- string started
- ok, see substring "/all_news"
- i replace after "/all_news" if nothing exist(if string ends "/all_news")
- i return "/all_news/pagex".
this'll it.
'/all_news/page1'.replace(/(.*\/all_news).*/,'$1' + '/pagex');
just 1 all.
java has lookbehind. negates need $1. solution looks like:
string result = "/all_news/page1"; string pattern = "(?<=\\/all_news).*"; system.out.println(result.replaceall(pattern,"/pagex"));
cheers.
Comments
Post a Comment