Assistance with REG EXP to match 5 digit number followed by undefined amount of text i.e 99999 AAA AAA etc

76 Views Asked by At

I am trying to create a simple regexp in JS and no matter how many times I try I can't seem to get it to work.

Using JS in AEM Designer

I am looking for the regexp to compare a user entry that:

  • must start with 5 digits (Any combination, no specific pattern)

  • followed by a single space

  • then have any combination of text and spaces I.E. CAR or CAR IS or CAR IS SUPER FAST or CARISSUPER FAST. Just looking for text, single space, text etc. However, it shouldn't allow for the text to end on anything other than a letter (no period or extra space or special character)

So far I have the 5 digits thing figured out and then a space, but I cannot seem to get the text to work the way I want.

Here is the code I am currently using in the form:

var pattern = new RegExp("^([0-9]{5}).([A-Z]+)$");
var result = Page1.Section1.MOSID.rawValue

if (pattern.test(result)) {
    xfa.host.setFocus(NextField);
} else {
    xfa.host.messageBox("Incorrect formatting, please follow format shown in caption");
}

This has gotten me part of the way there, but it only allows for 1 continuous line of text and doesn't allow spaces in between, nor does it account for the line ending.

I've been trying unsuccessfully with the following

("^([0-9]{5}).[A-Z]([.A-Z]*)$")
("^([0-9]{5}).([A-Z])([.A-Z]*)?$")
("^([0-9]{5})\s([A-Z])(\s\w+)*$")
("^([0-9]{5})\s([A-Z])+( \w+)*$")

I feel like I'm dancing around the solution but can't quite seem to get in step.

Any help would be appreciated.

2

There are 2 best solutions below

4
Barmar On BEST ANSWER
^\d{5}\s[A-Z\s]*[A-Z]$
  • ^ = beginning of string
  • \d{5} = 5 digits
  • \s = single space
  • [A-Z\s]* = any sequence of letters and spaces, possibly empty
  • [A-Z] = a single letter, so it can't end with a space
  • $ = end of string

DEMO

4
DDM On

Not sure what you're looking for, but a simple regexp in JavaScript that matches a 5 digit number followed by any amount of text, including whitespace will look like the following:

Updated expression:

/^\d{5} [a-zA-Z\s]*[a-zA-Z]$/, the expression will match strings that begin with 5 digits, followed by a space, and ends with a letter, with any combination of letters and spaces in between.

  • ^ : matches the start of the string
  • \d{5} : matches any 5 digits
  • : matches a single space
  • [a-zA-Z\s]* : matches any combination of letters and spaces
  • [a-zA-Z] : matches a single letter at the end of the string
  • $ : matches the end of the string

Old Ans:

/\d{5}\s*.*/

/\d{5} matches exactly five digits (0-9), \s* matches any amount of whitespace (spaces, tabs, etc.), .* matches any amount of text (including whitespace) after the 5 digit number and possible successful matches for this regexp: "12345", "12345 ", "12345abc", "12345 abc", "12345 abc def". This will also match the string "99999 AAA AAA".

What was wrong with your script ("^([0-9]{5}).([A-Z]+)$"):

  • The parentheses are generally used as a "capture group" which, in simple words, catches a match by grouping parts of the regular expression. In your code, it works as literal characters around the dot . in the middle of the expression.
  • Fix: the parentheses around the dot should be removed.
  • Secondly, the dot . in the middle of the expression will match only a single character, including whitespace and other characters that are not uppercase letters.
  • Fix: Replace the the dot with a character class to match any amount of whitespace and any uppercase letters.

Correct Solution Updated after making space compulsory: /^\d{5}\s+[A-Z\s]+$/i

Old Ans: /^[0-9]{5}\s*[A-Z\s]+$/