473,548 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExp FAQENTRY


ISTM that RegExps deserve a FAQ entry, with links to more detailed
sources.

An important question, probably not treated by many otherwise worthwhile
sources, must be on feature detection of the newer RegExp facilities -
for example, greedy/non-greedy.

The answer may be that it is not possible to do so in a safe manner;
that one can do no better than something like

document.write( "Testing non-greedy :- ")
X = /<trialRegExp>/.test(string)
document.write( "survived." )

That is, nevertheless, a useful answer; if it is right, it prevents the
naive seeking anything better, and if it is wrong someone will soon say
so.

Where a page requires an advanced RegExp facility, it is best to have a
controlled failure at a well-chosen point.

Putting something in the posted FAQ will provide an opportunity for
adding a reference to the Notes; and, without such a reference, their
value is much reduced.

--
© 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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #1
5 1807
Dr John Stockton wrote:
ISTM that RegExps deserve a FAQ entry, with links to more
detailed sources.

An important question, probably not treated by many otherwise
worthwhile sources, must be on feature detection of the newer
RegExp facilities - for example, greedy/non-greedy.

The answer may be that it is not possible to do so in a safe
manner; that one can do no better than something like

document.write( "Testing non-greedy :- ")
X = /<trialRegExp>/.test(string)
document.write( "survived." )

That is, nevertheless, a useful answer; if it is right, it
prevents the naive seeking anything better, and if it is
wrong someone will soon say so.

Where a page requires an advanced RegExp facility, it is
best to have a controlled failure at a well-chosen point.

<snip>

A little provisional testing suggest that the regular expression
features (such as non-greedy) may not be that difficult to feature
detect. Trying some reg ex syntaxes that should be problematic on older
browsers did not result in any errors, just different results. Which
means that using String.replace (at least) on a test string it should be
reasonable to assume that the regular expression implementation supports
the required feature if the resulting string equal the expected result.

The following examples list the operation used and the resulting strings
on various browsers, including a couple of dinosaurs.

"a".replace (/a??/, 'X')

HotJava3 Net 4 Opera 6 Opera 7 Mozilla 1.3 IE 6
X X a Xa Xa Xa
"aaaa".repl ace(/(a){2,4}?/, 'X')

HotJava3 Net 4 Opera 6 Opera 7 Mozilla 1.3 IE 6
X X aaaa Xaa Xaa Xaa
"aaaa".repl ace(/(a){2,}?/, 'X')

HotJava3 Net 4 Opera 6 Opera 7 Mozilla 1.3 IE 6
X X aaaa Xaa Xaa Xaa
"aaaab".replace (/a(?=b)/, 'X')

HotJava3 Net 4 Opera 6 Opera 7 Mozilla 1.3 IE 6
aaaab aaaab aaaab aaaXb aaaXb aaaXb
"aaaab".replace (/a(?:b)/, 'X')

HotJava3 Net 4 Opera 6 Opera 7 Mozilla 1.3 IE 6
aaaab aaaab aaaab aaaX aaaX aaaX
"aaaab".replace (/a(?!b)/, 'X')

HotJava3 Net 4 Opera 6 Opera 7 Mozilla 1.3 IE 6
aaaab aaaab aaaab Xaaab Xaaab Xaaab
Obviously I don't know for sure that these tests will not produce errors
on any browsers. It would certainly be worth seeing what IE 4 makes of
them.

Richard.
Jul 20 '05 #2
JRS: In article <c2************ *******@news.de mon.co.uk>, seen in
news:comp.lang. javascript, Richard Cornford
<Ri*****@litote s.demon.co.uk> posted at Sat, 6 Mar 2004 01:55:44 :-
Obviously I don't know for sure that these tests will not produce errors
on any browsers. It would certainly be worth seeing what IE 4 makes of
them.


All of them give me an "Internet Explorer Script Error" window,
"unexpected quantifier". Answering Yes, the return value appears to be
undefined, i.e. as var U gives.

So the tests can be used in IE4, with no more annoyance than a failure
in actual use would cause.

I take it that the last 3 browsers give the "proper" result.

ISTM that if browsers such as the first three are in use, then feature
testing before feature use is essential, otherwise the user is at risk
of being given a miscalculated result.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.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 #3
Dr John Stockton wrote:
Richard Cornford posted at Sat, 6 Mar 2004 01:55:44 :-
Obviously I don't know for sure that these tests will not produce
errors on any browsers. It would certainly be worth seeing what
IE 4 makes of them.
All of them give me an "Internet Explorer Script Error" window,
"unexpected quantifier". Answering Yes, the return value appears to
be undefined, i.e. as var U gives.


It is a pity that IE 4 generates the error report. I was actually
expecting the other three to generate error reports when they
encountered the unexpected qualifiers in the regular expressions (or
just stop executing scripts in the case of HotJava) and was pleasantly
surprised when they just created "incorrectl y" functioning RegExp
objects.

Even in the case of IE 4's error report the script can carry on under
control if the use clicks yes and a subsequent string comparison should
be able to tip the code off that it must not attempt to use the more
advanced regular expressions again. The worst case is no worse than
attempting to use the newer features untested and testing avoids placing
a reliance on the results of an "incorrectl y" functioning RegExp.
So the tests can be used in IE4, with no more annoyance than
a failure in actual use would cause.
Yes, and with Netscape 4 and Opera 6 still in fairly common use it has
got to be worth it.
I take it that the last 3 browsers give the "proper" result.
They are results consistent with my understanding of what the various
formulations of the RegExp used should have done (by ECMA 262 ed 3 &
Perl 5 specs).
ISTM that if browsers such as the first three are in use, then feature
testing before feature use is essential, otherwise the user is at risk
of being given a miscalculated result.


It is consistent with the feature detecting philosophy to verify the
feature (and in this case its functionality) before placing any reliance
on it. It would be better if there was a way to test without risking an
error. Ideally a RegExp formulation that was syntactically valid under
all levels of implementation but behaved differently. If there is such a
formulation I am not sufficiently familiar with regular expressions to
spot it.

Richard.

Jul 20 '05 #4
rh
Dr John Stockton <sp**@merlyn.de mon.co.uk> wrote in message news:<fr******* *******@merlyn. demon.co.uk>...
JRS: In article <c2************ *******@news.de mon.co.uk>, seen in
news:comp.lang. javascript, Richard Cornford
<Ri*****@litote s.demon.co.uk> posted at Sat, 6 Mar 2004 01:55:44 :-
Obviously I don't know for sure that these tests will not produce errors
on any browsers. It would certainly be worth seeing what IE 4 makes of
them.


All of them give me an "Internet Explorer Script Error" window,
"unexpected quantifier". Answering Yes, the return value appears to be
undefined, i.e. as var U gives.

So the tests can be used in IE4, with no more annoyance than a failure
in actual use would cause.


As I recall, IE4 supports window.onerror, so for this case you may
want to consider:

var onerrorSave = window.onerror;
window.onerror = catchError;
testRe = new RegExp("a??");
window.onerror = onerrorSave;

where the function catchError can provide graceful failure, or
invocation of alternative execution.

Assignment of window.onerror in browsers that don't support it should
be harmless.

..\rh
Jul 20 '05 #5
JRS: In article <29************ **************@ posting.google. com>, seen
in news:comp.lang. javascript, rh <co********@yah oo.ca> posted at Sun, 7
Mar 2004 09:30:59 :-

As I recall, IE4 supports window.onerror, so for this case you may
want to consider:

var onerrorSave = window.onerror;
window.onerror = catchError;
testRe = new RegExp("a??");
window.onerror = onerrorSave;

where the function catchError can provide graceful failure, or
invocation of alternative execution.

Assignment of window.onerror in browsers that don't support it should
be harmless.


The following alerts "Caught" and then "2 Before" :

<script>

function catchError() { alert('Caught') ; return true }

testRe = "Before"
var onerrorSave = window.onerror;
window.onerror = catchError;
testRe = new RegExp("a??");
window.onerror = onerrorSave;
alert("1 " + testRe) // not shown

</script>

<script>

alert("2 " + testRe)

</script>
Thus the offending statement has no effect, and the rest of the script
section is abandoned. For IE4, therefore, the test is satisfactory; it
needs to be demonstrated to be harmless elsewhere.

Netscape's web-site showed onError (big E), which does not serve for
that.

--
© 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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #6

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

Similar topics

3
7057
by: zzzxtreme | last post by:
hello i have this function to check date (not mine) function (s_date) { // check format if (!re_dt.test(s_date)) return false; // check allowed ranges if (RegExp.$1 > 31 || RegExp.$2 > 12) return false;
47
5033
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
4
7456
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as "PRACTICO". What's the best way to do this? TIA,
5
14448
by: Bert | last post by:
Hello, I'm having some problems with creating a script that works on both Mozilla browsers as IE. I want to change the background color of textareas and input text fields as soon as somebody has entered something in them. The script below works perfectly in mozilla browsers (netscape 7, firefox 1,...) but doesn't in internet explorer...
7
3427
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For example: var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/ var text = "There were some nesting parens in the test"; alert (regExpPos (text,...
7
16453
by: Bosconian | last post by:
I know that str.replace(/^\s+|\s+$/g,''); will trim a string of space, but what about removing extra spaces from the middle? Where "hello world"
19
3580
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and...
49
2358
$
by: google | last post by:
what dose a $ do in javascript, i have been given a bit of code and there are $'s all over the place, can someone explain their uses
31
3473
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I modify the current browser window? ----------------------------------------------------------------------- In a default security environment you are very limited in how much you can modify the current browser window. You can use ` window.resizeTo `...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.