Regular Expressions for HTML, CSS and JavaScript

Wiki RegEx

Textile Links to SWiki Links

"([^"]+)":([^ ]+)
[$1|$2]

Textile Label Embolden

^([a-zA-Z_-]+): 
*$1:* 

HTML RegEx

Ultimate Regular Expression for HTML tag parsing with PHP

<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>

<([a-z]+)\b((?!(style=|>)).)*>

HTML Entities

Replacing ‘ in words with apostrophes

([a-z]+)[’']([a-z]+)
$1&#039;$2

Replacing “ and “ before a word with left-double-quote

([\t ]+)["“]([a-z]+)
$1&ldquo;$2

Replacing “ and ” after a word with right-double-quote

([a-z]+)["”]([\t ]+)
$1&rdquo;$2

Replacing ‘ and ’ after numbers with primes

(\d+)[’']
$1&prime;

Replacing “ and ” after numbers with a Prime

(\d+)["”]
$1&Prime;

Replace “ and ” surrounded by spaces with a ditto mark

(\s+)["”](\s+)
$1&ditto;$2
〃 or U+3003

Strip newlines from around TD A pairs

(<td>)[\r\n]*(<a[^>]*>)[\r\n]*(.*?</a>)[\r\n]*(</td>)
$1$2$3$4

Capture a tag

(<{1}/?%TAG%{1})

Changing all uppercase HTML tags to lower case

<(/?)([A-Z]+)\b([^>]*)>
<$1\L$2\E$3>

\l	Lowercase next character
\u	Uppercase next character
\L	Lowercase until \E
\U	Uppercase until \E
\E	End case modification

Strip tag pairs down to the opening tag name and it’s attributes

<([a-z0-9]+)([^>]*)>.*
$1$2

Change XML/HTML attributes with single-quotes to double-quotes

(<.+?)([a-z-]+)='([^']+)'([^>]+>)
$1$2="$3"$4

Must be run multiple times.

(?<=<[a-z]+[\t ]+)([a-z-]+="[^"]+"[\t ]+)*?([a-z-]+)='([^']+)' BROKEN
$1="$2"

Remove tabs and extra whitespace from attribute values

([a-zA-Z-]+=["'][^\t"']*)(\t| [\t ])[\t ]*
$1 

Move tag class attribute to the front

<([a-z]+)(.*?)( class="[^"]+")>
<$1$3$2>

Finding an class name within HTML

class="[^"]*?CLASS_NAME\b[^"]*"

Move tag id attribute to the front

<([a-z]+)(.*?)( id="[^"]+")>
<$1$3$2>

CSS RegEx

Replace // with Proper CSS Comment Syntax

([\t ]*)//(.+)\n
$1/* $2 */\n

Replace = with : for CSS Attributes

([a-z]+)=
$1:

Wrap URLs in single quotes. Shouldn’t be needed

url\(([_a-z/.-]+)\)
url('$1')@

Properly Indent CSS

^([a-z-]+?: .+?;)
\t$1

\n\n([}\t])
\n$1

(.+?)\{
$1\n{