Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Regular expression search not working

Reply
Thread Tools

Regular expression search not working

 
 
donpro
Guest
Posts: n/a
 
      08-12-2011
Hi,

I have a TEXTAREA where I wish to ensure that it contains at least on
4 digit number. The following code snippet always returns -1 even when
the 4 digit number is present and I don;t know why. Note that I first
ensure that the vale is not empty but I am not showing that code in
the snippet below.

var hsCodeElement = document.getElementById('TaField));
var hsCodePattern = '/^\d{4}$/';

if (hsCodeElement.value.search(hsCodePattern) == -1) {
alert(hsCodeElement.value.search(hsCodePattern) + ": HS Codes must
contain at least entered one 4 digit numeric value");
setTimeout(function() {hsCodeElement.focus();}, 10);
}
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-12-2011
donpro wrote:

> I have a TEXTAREA where I wish to ensure that it contains at least on
> 4 digit number. The following code snippet always returns -1 even when
> the 4 digit number is present and I don;t know why. Note that I first
> ensure that the vale is not empty but I am not showing that code in
> the snippet below.
>
> var hsCodeElement = document.getElementById('TaField));
> var hsCodePattern = '/^\d{4}$/';


Drop the single quotes, a regular expression literal in Javascript s
delimited by slashes i.e.
var hsCodePattern = /^\d{4}$/;
I am not sure however you want the achors ^ and $ as you say "contains
at least on[sic] 4 digit number" while the expression you have written
basically says "is exactly a 4 digit sequence" and contains nothing
before or after that.


> if (hsCodeElement.value.search(hsCodePattern) == -1) {


if (hsCodePattern.test(hsCodeElement.value)) {

seems easier.


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
Denis McMahon
Guest
Posts: n/a
 
      08-12-2011
On Fri, 12 Aug 2011 09:48:12 -0700, donpro wrote:

> I have a TEXTAREA where I wish to ensure that it contains at least on 4
> digit number.


> var hsCodePattern = '/^\d{4}$/';


If you mean "at least one sequence of 4 digits"

var hsCodePattern = /\d{4}/;

If you mean "at least one sequence of exactly 4 digits"

var hsCodePattern = /(^|\s)\d{4}(\s|$)/;

a) Don't quote the regex, it's a regex, not a string containing a regex.
b) If you include start and end anchors, your pattern is treated more
like "consists of exactly 4 digits" rather than "contains a 4 digit
sequence".

This html will allow you to evaluate the contents of a text area against
4 different "find 4 digit" regexes.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Test RegExes</title>
<script type="text/javascript">
var regExes = new Array (/^\d{4}$/, /(^|\s)\d{4}(\s|$)/,
/\s\d{4}\s/, /\d{4}/);
function dgebi(id)
{
return document.getElementById(id);
}
function testRegExes()
{
var ta = dgebi('theTextArea');
var re, alert;
for (re = 0; re < regExes.length; re++)
{
alert = dgebi("alert_" + re);
if (ta.value.match(regExes[re]))
{
alert.style.backgroundColor = "#00ff00";
}
else
{
alert.style.backgroundColor = "#ff0000";
}
}
}
</script>
<style type="text/css">
p {margin: 20px auto 0 auto; text-align: center;}
table {margin: 20px auto 0 auto; border-collapse: collapse;}
th, td {text-align: center;border: thin solid black;}
</style>
</head>
<body onload="testRegExes()">
<p>
<textarea cols="60" rows="6" id="theTextArea"></textarea><br>
<input type="button" value="Evaluate" onclick="testRegExes()">
</p>
<table width="80%">
<tr>
<td width="25%" id="alert_0">/^\d{4}$/</td>
<td width="25%" id="alert_1">/(^|\s)\d{4}(\s|$)/</td>
<td width="25%" id="alert_2">/\s\d{4}\s/</td>
<td width="25%" id="alert_3">/\d{4}/</td>
</tr>
</table>
<table width="30%">
<tr>
<th width="100%">Color Key</th>
</tr>
<tr>
<td style="background-color: #00ff00">RegEx Matched</td>
</tr>
<tr>
<td style="background-color: #ff0000">RegEx Not Matched</td>
</tr>
</table>
</body>
</html>

Rgds

Denis McMahon
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      08-12-2011
12.8.2011 21:06, Denis McMahon wrote:

> If you mean "at least one sequence of exactly 4 digits"
>
> var hsCodePattern = /(^|\s)\d{4}(\s|$)/;


That's really "at least one sequence of exactly 4 digits surrounded by
whitespace or start or end of string"; for example, "a1234" does not
match, and neither does "1234-5" or "1234.".

If you want "at least one sequence of 4 digits that is not part of a
longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      08-12-2011
"Jukka K. Korpela" <(E-Mail Removed)> writes:

> If you want "at least one sequence of 4 digits that is not part of a
> longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.


Or /\b\d{4}\b/

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      08-12-2011
12.8.2011 22:25, Lasse Reichstein Nielsen wrote:

> "Jukka K. Korpela"<(E-Mail Removed)> writes:
>
>> If you want "at least one sequence of 4 digits that is not part of a
>> longer digit sequence", then the pattern /(^|\D)\d{4}($|\D)/ would do.

>
> Or /\b\d{4}\b/


No, that would be different; "a1234" would not match.


--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search regular expression with search for hex values in files? Peter Hanke Perl Misc 1 01-06-2008 08:54 PM
Regular Expression Negation not working In C++. sk.rasheedfarhan@gmail.com C++ 6 02-21-2007 06:20 PM
Regular Expression Validator - Server side validation is not working CK ASP .Net 3 05-16-2006 06:21 PM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments