user3643194
user3643194

Reputation: 1

str replace all in Javascript

I am trying to some some urls throught javascript where some replacement of urls needs to be done. I have a textarea with some URLs example given below:

http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3

Now what i am trying to do is replacing http://mywebsite.com/preview.aspx?mode=desktop&url= with spaces.

I have tried using str.replace() but it is replacing only first occurence of that url. I have also tried with Global variable g the query i have used is

str_replace(\http://mywebsite.com/preview.aspx?mode=desktop&url=/g,'');

But its not working So can anyone tell me how i can do that ?

I want the output of the textarea like:

http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/post.aspx?id=44&content=4

Upvotes: 0

Views: 51

Answers (2)

Kiel
Kiel

Reputation: 493

try this.

var string = document.getElementById('textareaidhere');
 string.replace(/http:\/\/mywebsite\.com\/preview\.aspxmode=desktop&url=/g, '');

JSFiddle here

Upvotes: 0

Jason Tolliver
Jason Tolliver

Reputation: 289

I believe that your biggest issue is that your regex syntax is incorrect. Try this:

Imagine that var s is equal the the value of your textarea.

s.replace(/http\:\/\/mywebsite\.com\/preview.aspx\?mode\=desktop\&url\=/g, '');

The issue you were having was improper delimiters and unescaped reserved symbols.

Though Javascript has some of its own regex idiosyncrasies, the issues here were related to basic regex, you might find these resources useful:

http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

http://regexpal.com

Upvotes: 2

Related Questions