472,983 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

String.replace(/</g,'&lt;');

Three questions

1)

I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I supposed to write this function?

String.replace(/</g,'&lt;');

2)

While I'm on the subject, anyone know why they implemented replace using
a slash delimiter instead of quotes? I know it's how it's done in Perl
but why is it done that way?

3)

One last regexp question:
is it possible to do something like this:
String.replace(/<(.*?)>(.*?)</$1>/ig,'&lt;$1&gt;$2&lt;/$1&gt;');
This is just an example where a sub-match used in a regular expression
must sub-match again exactly as it did the first time later in the same
string. But I don't know how to do that in a regexp although it seems
like it should be possible.
Jul 20 '05 #1
4 61941
higabe <hi****@hotmail.com> writes:
Three questions

1)

I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I supposed to write this function?

String.replace(/</g,'&lt;');
Hmm, I can see that I have some of those too, the most recent of them
written today. Bummer. I never noticed that there was a </-sequence in
that.
Try
String.replace(/[<]/g,'&lt;');
or
String.replace(RegExp("<","g"),'&lt;');
2)

While I'm on the subject, anyone know why they implemented replace using
a slash delimiter instead of quotes? I know it's how it's done in Perl
but why is it done that way?
They didn't implement "replace" with slash-delimiters. They
implemented *regular expressions* with slash-delimiters. You can use
regular expressions in many other ways than just string-replace.

You could also write
var myRegExp = /[<]/g;
String.replace(myRegExp,'&lt;');

These are equivalent uses of regular expressions and strings:
/a*b/i.exec("caabc")
and
"caabc".match(/a*b/i)

3)

One last regexp question:
is it possible to do something like this:
String.replace(/<(.*?)>(.*?)</$1>/ig,'&lt;$1&gt;$2&lt;/$1&gt;');
Yes, but you need to escape the slash in "</" and it's "\1" instead of
"$1". Also you will only want to match the tag name, not attributes,
and you have no letters, so the "i" flag is not necessary. And don't
call a variable "String", since it conflicts with the global variable
holding the constructor of String objects.

So, this should do what you wanted:

string.replace(/<\s*(\w+)\b(.*?)>(.*?)<\/\1>/g,
'&lt;$1$2&gt;$3&lt;/$1&gt;');

It is confuzed if ">" occurs inside an attribute, e.g. <tag
attr="foo>bar">. Just don't do that :)

It doesn't handle nested tags either. That is still outside the power
of regular expressions, even with backreference.

There are ways around that, though, using a function as second argument
of replace, allowing us to use recursion:

function tagify(string) {
return string.replace(/<\s*(\w+)\b(.*?)>(.*?)<\/\1>/g,
function(match,sub1,sub2,sub3) {
return "&lt;"+sub1+sub2+"&gt;" +
tagify(sub3) +
"&lt;/"+sub1+"&gt;";
});
}

This still fails for elements with no closing tag. It could probably
be made to work for XHTML, where all tags have end tags (sometimes
abbreviated to just end in "/>"):
/<\s*(\w+)\b(|.*?[^/])(?:\/>|>(.*?)<\/\1>)/g

^start tag
^optional whitespace
^tagname
^optional attributes, not ending in /
^either >content</tagname> or just />

The XHTML parser would then be:

function tagify(string) {
return string.replace(
/<\s*(\w+)\b(|.*?[^/])(?:\/>|>(.*?)<\/\1>)/g,
function(match,sub1,sub2,sub3) {
return "&lt;"+sub1+" "+sub2+
(sub3 !== undefined ?
"&gt;" + tagify(sub3) +
"&lt;/"+sub1+"&gt;" :
"/>");
});
}
Hmm. I feel stupid, considering the much larger parser for XHTML that
I made some time ago. Oh well, at least it handled ">" inside
attribute values :).
This is just an example where a sub-match used in a regular expression
must sub-match again exactly as it did the first time later in the same
string.
It works in recent versions of Javascript/ECMAScript. Earlier ones didn't
have non-greedy matches (*?) or backreferences (\1).
But I don't know how to do that in a regexp although it seems
like it should be possible.


It is, and you were close.
Adding backreferences to regular expressions gives them more power than
"real" regular expressions, i.e., they can be used to match something that
is not a regular language. Example:

/^(11+)\1+$/

This regular expression matches any string of 1's that can be written
as two or more repetitions of two or more 1's. That is, unary representation
of composite numbers.
!/^(11+)\1+$/.test("--string of n 1's--")
is a test for whether n is prime (but not a very efficient one).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
JRS: In article <3F***************@hotmail.com>, seen in
news:comp.lang.javascript, higabe <hi****@hotmail.com> posted at Mon, 17
Nov 2003 23:03:18 :-
I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I supposed to write this function?

String.replace(/</g,'&lt;');


String.replace(/\</g,'&lt;'); appears acceptable to MSIE 4. It may,
however, be deprecated; you could try \x3c and \o74 to replace < .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
JRS: In article <3F***************@hotmail.com>, seen in
news:comp.lang.javascript, higabe <hi****@hotmail.com> posted at Mon, 17
Nov 2003 23:03:18 :-
I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I supposed to write this function?

String.replace(/</g,'&lt;');
String.replace(/\</g,'&lt;'); appears acceptable to MSIE 4.


The original is also acceptable to all the browsers I have access to,
and they are equally incorrect according to the HTML 4 specification.
The problem is the character sequence "</", and it is still there.
It may, however, be deprecated; you could try \x3c and \o74 to
replace < .


I havent heard of \o??. Do you mean octal \074?
It should work then (or \u003c). In either case it is a Javascript
escape for the character, so the HTML parser won't see "</".

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
JRS: In article <sm**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Tue, 18 Nov 2003 20:32:47 :-
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
JRS: In article <3F***************@hotmail.com>, seen in
news:comp.lang.javascript, higabe <hi****@hotmail.com> posted at Mon, 17
Nov 2003 23:03:18 :-
I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I supposed to write this function?

String.replace(/</g,'&lt;');
String.replace(/\</g,'&lt;'); appears acceptable to MSIE 4.


The original is also acceptable to all the browsers I have access to,
and they are equally incorrect according to the HTML 4 specification.
The problem is the character sequence "</", and it is still there.


Oops.

It may, however, be deprecated; you could try \x3c and \o74 to
replace < .


I havent heard of \o??. Do you mean octal \074?


It (\o??) is/was in the NS reference page for RegExp, "Last Updated:
05/28/99 12:00:15 "; the little o can be replaced, in my browser, by big
o or by zero. There, using \0 (zero) is rather like using \1 or \2,
which can be *either* octal or back-references.

"Last Updated September 28, 2000" omits \o and says that \0 must not be
followed by another digit (matches NUL character). "octal" does not
occur in the page, nor in the RegExp region of ECMA-262 3rd Edn.

Hence, while \o74 \O74 \074 may work, \x3c should be used instead of
them.
It should work then (or \u003c). In either case it is a Javascript
escape for the character, so the HTML parser won't see "</".


--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: baobaoba | last post by:
Hi, I have a piece of code to do string replacement : #include <iostream> #include <string> using namespace std; void charEscape (string& src) { const string delims("&<");...
15
by: tshad | last post by:
How do I go about this? I used to know this, but can't find VB.net replace that does this. Something like string.replace("<br>",NL) Thanks,
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
4
by: Craig Buchanan | last post by:
If I have a string variable, is there a way to get the Replace method to work on *its* contents, without having to dimenstion a second variable? Something like: Dim MyTest as String = "<hello/>"...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
5
by: dolittle | last post by:
Hi, I have a user text. Some of the strings in the text could be longer than my display so I thought of using string.replace with regex to insert <wbr> tags after a fixed number of characters. For...
3
by: Alun | last post by:
I need to replace all new line characters in a string with a valid XHTML line break tag <br />. I'm trying to use the string.Replace method for this. Here's some example code: String...
3
by: Sin Jeong-hun | last post by:
If I use something like this, string html = "<h1>C# is great</h1>"; Console.WriteLine(html.Replace("&lt;","<").Replace("&gt;",">")); Does this recreate new string objects two times, even though it...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.