0

i want to assign src attribute to alt attribute with removing extension .png and removing src attribute.

string1='<img src="a.png"> hi '

to :

string2='<img alt="a"> hi '

how to do this? what is code?

4
  • Is the string really this simple HTML fragment, or is it actually more complex? And what are you going to do with the string result? Is it ultimately going to become a DOM element? Commented Jan 2, 2011 at 21:59
  • it s not complex. i like to remove tags. result can be 'img:a hi' Commented Jan 2, 2011 at 22:06
  • Is it really src=="a.png"? (two =?) Commented Jan 2, 2011 at 22:06
  • What is the purpose of this? Also, note that src is a required attribute in both HTML4 and HTML5: w3.org/TR/html5/embedded-content-1.html#attr-img-src Commented Jan 2, 2011 at 22:13

2 Answers 2

1
string1.replace(/src=([^.]+).png/,"alt=$1");

However, there is probably a cleaner way to accomplish what you are trying to do using DOM.

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

Comments

0

If you want to restrict the search-and-replace to the image element, you need to include img in the pattern:

string1.replace(/(<img [^>]*)src="([^.]+).png"/,'$1alt="$2"');

This has the added benefit of keeping your other attributes in tact.

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.