2

Is there a way to select all <br> tags that follow a paragraph with a given class? i.e. <p class="myclass">This is a paragraph</p><br>

There may be other <br> in the HTML so I cannot use this:

br {display:none;} 

and I cannot delete all <br> tags. If there is a way to select these particular <br> tags then I can use CSS.

There are about 700 pages and I do not want to go through each of them to make sure if the <br> is needed or not. I do know that it is not needed following a paragraph with the class of "myclass".

If there is no way to select these tags then I think that I can use BBEdit to do the search and replace using a regular expression. But I don't know how to write the RE that would work.

TIA, Linda

Linda
  • 1,973
  • 4
  • 21
  • 24
  • sorry, what has this got to do with regexes? and what is stopping you from using `br.myclass {display:none;}` – tobyodavies Oct 19 '10 at 23:16
  • @tobyodavies did you even read the whole question? The regex would be used as a filter to find all the br elements that need to be deleted in the web site code. Additionally, br.myclass would not work as a selector given the example @Linda provided. – Moses Oct 19 '10 at 23:29
  • The question said 'select all br tags with a given class', and the example didn't make a lot of sense to me... tho your answer clears it up... and i _still_ don't see what its got to do with regexes – tobyodavies Oct 19 '10 at 23:32
  • Couldn't you just use the normal find and replace dialogue to find `
    ` and replace with ``? EDIT: see my answer - it's not programming, but since you stipulated you're using BBEdit it should help
    – nearlymonolith Oct 20 '10 at 00:16

3 Answers3

2
p.myClass+br {display:none;}

This will select all <br> elements that are directly adjacent to a <p class="myClass"> element. If you need anything more dynamic than that, you will need regex.

Moses
  • 9,033
  • 5
  • 44
  • 67
0

Assuming BBEdit is similar to TextWrangler, you could use the built in Find dialogue.

Go to Search > Find... (Command + F), do "Seach For" </p><br> and "Replace With" </p> and then use the "Multi-file search" option at the bottom of the window to choose your files.

This isn't a regex, but since you said you're using BBEdit, which is made by Bare Bones and supposedly shares a lot with TextWrangler, it should work. (Otherwise just download TW for free). It even gives you a nice pop-up telling you what it found and replaced in case you want to review, etc.

See this page for more info on BBEdit's search and other fun features.

nearlymonolith
  • 946
  • 4
  • 7
0

Supposing you want to use regex to delete all <br> tags that follow a paragraph with a class named myclass:

Search for: (<p\b[^><]\sclass\s=\s*["']?myclass["']?[^><]>.?<\/p>\s*)<br\s*/?>

Replace with: $1

Note, you must ensure that all p tags in your HTML documents are properly closed.

Vantomex
  • 2,247
  • 5
  • 20
  • 22