How to use a regular expression to match this string:
<div><span>regular</span><span>expression</span></div>
.How to exchange the first span of the contents and the contents of the second span.
In my opinion, it can use jquery source code.
How to use a regular expression to match this string:
<div><span>regular</span><span>expression</span></div>
.
How to exchange the first span of the contents and the contents of the second span.
In my opinion, it can use jquery source code.
Make sure you read RegEx match open tags except XHTML self-contained tags
Then the solution to you problem would be:
var str = '<div><span>regular</span><span>expression</span></div>';
var newString =
str.replace(
/(<div><span>)([^<]*)(<\/span><span>)([^<]*)(<\/span><\/div>)/i,
'$1$4$3$2$5'
); // where /[^<]*/ matches anything but '<'
// results in "<div><span>expression</span><span>regular</span></div>"
Whitespace-safe regex:
/(<\s*div\s*>\s*<\s*span\s*>)([^<]*)(<\/\s*span\s*>\s*<\s*span\s*>)([^<]*)(<\/\s*span\s*>\s*<\/\s*div\s*>)/i
But honestly this is just a vague answer to you vague question and might need a bit more jQuery :)