Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression 1

Status
Not open for further replies.

east666

Programmer
Oct 5, 2006
3
GB
Hi All,

I am wanting to search a string for the occurence of an expression, and then change the expression in the string.

I need to search for HTML entities, such as Beta(β), Trademark(™)....etc

Once one has been found, I want to replace the beginning "&" with say "??" or something else I chose.

So, I need a function to search for say (β) and return (??beta;).

I know this uses regular expressions, but I am not to good on the syntax.

Please can anyone help.

Many Thanks.
 
Actually, use this one instead. The first one might mess up if there is an ampersand in your string that is not part of an HTML entity.
Code:
function escapeEntities(str){
  return str.replace(/&([A-Za-z]+;)/g,"??$1");
}

Adam
 
Thanks Adam.

I changed it slightly to be:

return str.replace(/&([A-Za-z]+;)/g,"??$1");

as it was picking up normal (&) by themselves. So it works the way I expected. Do you think this is the right way to do it.
 
Hi Adam,

Yes, you are correct in what you are saying. This is because basically we have a CMS that uses Microsofts DHTML Active X component for the Text Editor.

When I paste the relevant HTML entities into the editor, such as (β), and save it, the CMS copies the contents of the editor to a hidden form field, and through various xhtml filters before it gets saved. Unfortunately, this procedure cannot be changed, as it is in-built in our CMS, at the moment.

But it seems that when we automate the copy, via Javascript, from the editor, to the hidden form field (a simple "document.form.strtext.value = document.htmltext.DocumentHTML;" command), all instances of (&) are changed to (&). This is I feel a trait of the Active X editor, so I am trying to change all HTML Entities to something else, before I save them, then change them back when they are read out of the DB.

Maybe all very long winded, but I have it working perfectly, thanks again for your help. Do you have any other thoughts on getting around this?
 
Javascript has built this in for you already, in 2 easy commands:

Code:
<script type="text/javascript">
var a = "ab c d we $#@^@ &(*(*^%";
alert("original string: " + a);
var b = [!]escape[/!](a);
alert("escaped string: " + b);
var c = [!]unescape[/!](b);
alert("unescaped string: " + c);
</script>

Just escape the string before you save it to the database, and unescape it when you pull it out.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top