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!

RegExp opional digit amount 2

Status
Not open for further replies.

MarkZK

Technical User
Jul 13, 2006
202
GB
Hi,
Could someone that knows regexp tell me how to check the height attribute, I'm not sure if this is the best way to go about it but here is what I have so far,
var hght = /(")?\d{3}(%?)(")?/g;

whether there are quotes is optional (height="100" height=100%) and the same applies for the percent sign, the problem I'm having is allowing for one digit, two digits, and three digits from one to nine but no higher (highest value = 999 lowest = 1), I hope I explained that right, Thanks
 
Hi cLFlaVA, yours works much better than mine, but when I enter the value "1000%" it returns "100,0%" instead of null, still at least I can enter "50%" now, Thanks.
 
I'm just testing it right now, all I'm using is this,

<script type="text/javascript">
a = '"100%"';
var hght = /\"?\d{1,3}\%?\"?/g;
//var hght = /(")?\d{3}(%?)(")?/g;
document.write(a.match(hght));
</script>
 
i think what you're looking for is this:

Code:
<script type="text/javascript"><!--
var a = '"100%"';
var hght = /^\"?\d{1,3}\%?\"?$/;
//var hght = /(")?\d{3}(%?)(")?/g;
document.write(hght.test(a));
//--></script>



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I think you're right, Thanks a lot .
 
Just to further explain, when you use the match method it returns an array of information about the match. In the case of your original test, it found 2 matches in your string:
Code:
"[b][COLOR=blue]100[/color][COLOR=red]0%[/color][/b]"
As you can see, the blue part matches the regular expression, between 1 and 3 numbers, with or without a percentage sign. And the red also meets the same criteria. So, it sends you back a 2 element array that says "these substrings met the criteria for a match".

As cory suggested you can use the test method instead which returns a boolean value of true or false, indicating whether or not any match was made.

-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