How to select all style attributes and all their content with regex in a source code editor?

Question had an internal problem and is edited: I want to select all content whatsoever inside a <p> tag via a source code editor and change it that selection to something else.

How would you suggest to do that?

This worked for me in regex mode in Virtual Studio Code:

<p.*>

.* is greedy by default. Make it non-greedy.

Either one of the first two will work. Depends on if you’re trying to catch the style as a separate match from the overall.

style=".*?"
style="(.*?)"

Also keep in mind that you’re matching the double-quotes literally; if any of your style tags use single quotes, they won’t be matched.

1 Like

It works for me using VSCode… are you sure you’ve clicked the thing on the right of the search bar to tell it to use regexes?

1 Like

by passed over you mean the match included the align?

if you mean it skipped the align and you think it shouldnt… you wanted the contents of the style attribute. align is a different, separate attribute.

1 Like

I forgot to click the button you have mentioned.

I meant “skipped”.
I confused align="right" with a possible text-align: right; because indeed my original aim was to select everything within a <p> tag, not only the style attribute. Please forgive me for misleading you and I have edited the question and deleted comments to make a useful post for possible future readers.

1 Like

Something like this perhaps

<p[^>]+>(.*?)<\/p>

example here

<p[^>]+> opening p tag e.g. <p class='myClass1'>
(.*?) capture group, to capture the contents of the paragraph
<\/p> closing tag

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.