RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find (X)HTML & char. replace
Expression
&(?![a-z]+;|#\d+;)
Description
Single & character is not valid in HTML or XHTML code. In this case you can replace this character with its entity (if & character is not member of other entity [š]). example: preg_replace('/&(?!amp;)/', '&', $str). (thanks to mr. Mrva)
Matches
&
Non-Matches
&|"|š|&blah;|©
Author Rating: Not yet rated. Martin Švec
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Close but not perfect
Name: Michael Ash
Date: 5/10/2007 11:19:07 AM
Comment:
Entities can also be written using hexadecimal notation which your regex doesn't account for. Also you should be careful when using the pound character in a regex as turning on the ignorewhitespace option can break your regex. Here's a regex I wrote a few years back to do the same thing. It put a limit of the range of values of the entities. &(?!(?:\#((x([\dA-F]){1,5})|(104857[0-5]|10485[0-6]\d|1048[0-4]\d\d|104[0-7]\d{3}|10[0-3]\d{4}|0?\d{1,6}))|([A-Za-z\d.]{2,31}));)


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials