Home > Net >  How to move a span tag in an html page multiple times efficiently with find-and-replace or regex or
How to move a span tag in an html page multiple times efficiently with find-and-replace or regex or

Time:11-29

I have a very lengthy html page with multiple instances of this type of code:

<h3>Some header text
    <span >Text I want to move</span>
</h3>

Ideal result:

<p>Text I want to move</p>
<h3>Some header text</h3>

I can use regex to find all the spans, but then what? Replace doesn't help me since the content is unique each time.

Thanks for your help, and let me know if I left any crucial info out.

Edit: Per @bloodyKnuckles's comments, adding some more information: I'm on mac, editing an html locally in Adobe Dreamweaver that will be copied onto a remote server later. The pages already exist there and I am updating some of the code. In terms of regex, I do not have a lot of experience with it, and I tried a couple of different processors but I can't recall exactly which ones.

CodePudding user response:

I use Sublime Text for programming on my Mac. (VIM when on the server.) They have a regex-capable find and replace feature. Here's what work with them:

 

Find: <h3>(. )\n *(<span >[^<]*</span>)\n</h3>

Replace: \2\n<h3>\1</h3>

 

Before:

<h3>Some header text
    <span >Text I want to move</span>
</h3>

<h3>Another header text
    <span >Something else.</span>
</h3>

After:

<span >Text I want to move</span>
<h3>Some header text</h3>

<span >Something else.</span>
<h3>Another header text</h3>

 

Looks like Dreamweaver uses something like this:

Find: <h3>(. )\n *(<span >[^<]*</span>)\n</h3>

Replace: $2\n<h3>$1</h3>

CodePudding user response:

Based on the link shared by @bloodyKnuckles, I saw this useful tip:

Use parentheses to set off groupings within the regular expression to be referred to later. Then use $1, $2, $3, and so on in the Replace With field to refer to the first, second, third, and later parenthetical groupings.

Finally, my solution worked like this-

Find:

(<h3.*)(<span >*. <\/span>)(</h3>)

Replace with:

$2$1$3

Thanks so much, everyone, and thank you @bloodyKnuckles for pointing me in the right direction.

  • Related