I’ve a string done like this: “http://something.org/dom/My_happy_dog_%28is%29cool!”
How can I remove all the initial domain, the multiple underscore and the percentage stuff?
For now I’m just doing some multiple replace, like
str = str.replace("http://something.org/dom/","");
str = str.replace("_%28"," ");
and go on, but it’s really ugly.. any help?
Thanks!
EDIT:
the exact input would be “My happy dog is cool!” so I would like to get rid of the initial address and remove the underscores and percentage and put the spaces in the right place!
The problem is that trying to put a regex on Chrome “something goes wrong”. Is it a problem of Chrome or my regex?
I’d suggest:
JS Fiddle demo.
The reason I took this approach is that RegEx is fairly expensive, and is often tricky to fine tune to the point where edge-cases become less troublesome; so I opted to use simple string manipulation to reduce the RegEx work.
Effectively the above creates a substring of the given
strvariable, from the index point of thelastIndexOf('/')(which does exactly what you’d expect) and adding1to that so the substring is from the point after the/not before it.The regex:
(_)matches the underscores, the|just serves as anoroperator and the(%\d{2,})serves to match digit characters that occur twice in succession and follow a%sign.The parentheses surrounding each part of the regex around the
|, serve to identify matching groups, which are used to identify what parts should be replaced by the' '(single-space) string in the second of the arguments passed toreplace().References:
lastIndexOf().replace().substring().