0

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, we would be thrilled if you marked it as accepted :)
0

try this.

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

JSFiddle here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.