admin管理员组

文章数量:1335125

I'm trying to match feet and inches but I can't manage to get "and/or" so if first half is correct it validates:

Code: (in javascript)

var pattern = "^(([0-9]{1,}\')?([0-9]{1,}\x22)?)+$";

function testing(input, pattern) {
        var regex = new RegExp(pattern, "g");
        console.log('Validate '+input+' against ' + pattern);
        console.log(regex.test(input));
    }

Valid tests should be:

  • 1'
  • 1'2"
  • 2"
  • 2 (assumes inches)

Not valid should be: * anything else including empty * 1'1'

But my regex matches the invalid 1'1'.

I'm trying to match feet and inches but I can't manage to get "and/or" so if first half is correct it validates:

Code: (in javascript)

var pattern = "^(([0-9]{1,}\')?([0-9]{1,}\x22)?)+$";

function testing(input, pattern) {
        var regex = new RegExp(pattern, "g");
        console.log('Validate '+input+' against ' + pattern);
        console.log(regex.test(input));
    }

Valid tests should be:

  • 1'
  • 1'2"
  • 2"
  • 2 (assumes inches)

Not valid should be: * anything else including empty * 1'1'

But my regex matches the invalid 1'1'.

Share Improve this question edited Feb 27, 2019 at 19:46 hc_dev 9,4281 gold badge30 silver badges42 bronze badges asked Jan 21, 2016 at 9:59 KrillkoKrillko 6451 gold badge10 silver badges22 bronze badges 1
  • Tip: Do not use /g with a regex that is used in RegExp#test(). – Wiktor Stribiżew Commented Jan 21, 2016 at 10:07
Add a ment  | 

3 Answers 3

Reset to default 6

Remove the + at the end (which allows more than one instance of feet/inches right now) and check for an empty string or illegal entries like 1'2 using a separate negative lookahead assertion. I've also changed the regex so group 1 contains the feet and group 2 contains the inches (if matched):

^(?!$|.*\'[^\x22]+$)(?:([0-9]+)\')?(?:([0-9]+)\x22?)?$

Test it live on regex101..

Explanation:

^          # Start of string
(?!        # Assert that the following can't match here:
 $         # the end of string marker (excluding empty strings from match)
|          # or
 .*\'      # any string that contains a '
 [^\x22]+  # if anything follows that doesn't include a "
 $         # until the end of the string (excluding invalid input like 1'2)
)          # End of lookahead assertion
(?:        # Start of non-capturing group:
 ([0-9]+)  # Match an integer, capture it in group 1
 \'        # Match a ' (mandatory)
)?         # Make the entire group optional
(?:        # Start of non-capturing group:
 ([0-9]+)  # Match an integer, capture it in group 2
 \x22?     # Match a " (optional)
)?         # Make the entire group optional
$          # End of string

try this

var pattern = "^\d+(\'?(\d+\x22)?|\x22)$";

Not to resurrect the dead, but here was my best shot at detecting fractional feet and inches. It will find:

  • 3'
  • 3'-1" or 3' 1"
  • 3'-1 1/2" or 3' 1 1/2"
  • 3'-1/2", 3' 1/2", 3'-0 1/2", or 3'0 1/2"
  • 1"
  • 1/2"

The only catch is your flavor of regex has to support conditionals.

pattern = "(\d+')?(?:(?(1)(?: |\-))(\d{1,2})?(?:(?(2) )\d+\/\d+)?\x22)?"

本文标签: Regex (JavaScript) match feet andor inchesStack Overflow