1

Assume I have the following URL stored in variable called content:

http://www.example.com/watch?v=4444444&feature=related

Problem:

  1. I need to replace watch?v= with embed/
  2. I need to erase whatever comes after &

The final output would look like:

http://www.example.com/embed/4444444

I tried these two steps but didn't work:

content = content.replace('/watch?v=/', 'embed/');
content = content.replace('&*/g','');

The URL in page source code appears as:

http://www.example.com/watch?v=4444444&feature=related

1 Answer 1

3

You have many errors:

  • You are using a regular expression when you only need a string.
  • You are writing your regular expressions as strings.
  • To write 'match any characters' you need to write '.*', not just '*'. The star modifies the previous token.
  • There is no need to use the g flag here.

Try this instead:

 content = content.replace('watch?v=', 'embed/').replace(/&.*/, '');
Sign up to request clarification or add additional context in comments.

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.