Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
src=="a.png"
=
src
string1.replace(/src=([^.]+).png/,"alt=$1");
However, there is probably a cleaner way to accomplish what you are trying to do using DOM.
Add a comment
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.
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
src=="a.png"? (two=?)srcis a required attribute in both HTML4 and HTML5: w3.org/TR/html5/embedded-content-1.html#attr-img-src