Sublime Forum

Regex: Put a comma on REPLACE html tags

#1

hello. Maybe you can help me. So, I have these 2 html tags:

<title>My name is Prince | Always</title>

<meta name="keywords" content="laptop, home, yellow, diamond"/>

With the regex below I can replace the content of the <title></title> tag with the content of <meta name=..> tag

. matches newline:

Search: (<title>(.*?)<\/title>.*?)(<meta name="keywords" content=").*?("\/>)

REPLACE BY: \1\3\2\4

BUT, I need to put a comma between words, after replace, on the ` tag

So, the output should be:

<meta name="keywords" content="my, name, is, prince, always"/>

Can anyone help me?

0 Likes

#2

You basically want a custom callback function for matched groups. In short, it’s not a (ST’s built-in) regex job. Or you have to execute a regex for uncertain times.

You would have to write a simple script/program to do that. Maybe shell piping can do that too, but a script/program is my first though.

1 Like

#3

a friend gives me a solution, but I must replace multiple times.

So, after I made the SEARCH and REPLACE with my regex, I should do another REPLACE with this regex:

  • FIND WHAT = (content=")(.*?(?<!,))\x20\|?\x20*(.*)
  • REPLACE WITH = $1\l$2,\x20\l$3

WORKS ! But it’s a bit cumbersome

0 Likes

#4

you can find the answer here:

or here:

0 Likes