Thian Kian Phin
Thian Kian Phin

Reputation: 931

replace function to replace single quote string

I have this string something='http://example.com/something' and how can I replace the something=' with nothing?

when I do str.replace('something='','') I got syntax error. I tried str.replace('something=\'','') and expect to escape the single quote with slash, it doesn't work too.

Upvotes: 0

Views: 416

Answers (3)

Pranav C Balan
Pranav C Balan

Reputation: 115202

You need to update the str variable with the returned value since String#replace method doesn't update the variable.

str = str.replace('something=\'', '')

Although it's better to use double quotes instead of escaping.

str = str.replace("something='", '')

Upvotes: 1

baao
baao

Reputation: 73211

I believe what you are looking for is a replacement of something=' and all ticks (') including the closing one... So you could use this:

var str = "something='http://example.com/something'";

alert(str.replace(/something='(.*)'/, "$1"));

Upvotes: 2

Psi
Psi

Reputation: 6783

str.replace('something='','') will of course lead to a syntax error.

Try

str.replace("something='","")

Upvotes: 3

Related Questions