Enrichman
Enrichman

Reputation: 11337

Javascript regex expression to replace multiple strings?

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?

Upvotes: 0

Views: 1509

Answers (5)

David Thomas
David Thomas

Reputation: 253486

I'd suggest:

var str =  "http://something.org/dom/My_happy_dog_%28is%29cool!";
str.substring(str.lastIndexOf('/')+1).replace(/(_)|(%\d{2,})/g,' ');

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 str variable, from the index point of the lastIndexOf('/') (which does exactly what you'd expect) and adding 1 to that so the substring is from the point after the / not before it.

The regex: (_) matches the underscores, the | just serves as an or operator 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 to replace().

References:

Upvotes: 1

MARP
MARP

Reputation: 581

ok, if you want to replace all that stuff I think that you would need something like this:

/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g

test

var string = "http://something.org/dom/My_happy_dog_%28is%29cool!";
string = string.replace(/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g,"");

Upvotes: 0

Gene
Gene

Reputation: 47020

You haven't defined the problem very exactly. To get rid of all stretches of characters ending in %<digit><digit> you'd say

var re = /.*%\d\d/g;
var str = str.replace(re, "");

Upvotes: 0

user1510712
user1510712

Reputation: 41

Maybe you could use a regular expression to pull out what you need, rather than getting rid of what you don't want. What is it you are trying to keep?

You can also chain them together as in:

str.replace("http://something.org/dom/", "").replace("something else", "");

Upvotes: 1

Jay
Jay

Reputation: 3305

You can use unescape to decode the percentages:

str = unescape("http://something.org/dom/My_happy_dog_%28is%29cool!")
str = str.replace("http://something.org/dom/","");

Upvotes: 1

Related Questions