英文:
Assistance with REG EXP to match 5 digit number followed by undefined amount of text i.e 99999 AAA AAA etc
问题
我试图在JS中创建一个简单的正则表达式,无论我尝试多少次,似乎都无法使其工作。
在AEM Designer中使用JS
我正在寻找用于比较用户输入的正则表达式,该输入必须:
-
必须以5个数字开头(任何组合,没有特定模式)
-
后跟一个空格
-
然后可以是文本和空格的任何组合,例如CAR或CAR IS或CAR IS SUPER FAST或CARISSUPER FAST。只想要文本、单个空格、文本等。但不应允许文本以字母以外的任何字符结尾(不允许句点、额外的空格或特殊字符)
到目前为止,我已经解决了5位数字和一个空格的问题,但我似乎无法使文本按我想要的方式工作。
以下是我目前在表单中使用的代码:
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("格式不正确,请按标题中显示的格式填写");
}
这让我部分成功,但它只允许一个连续的文本行,不允许文本之间有空格,也不考虑行尾。
我一直在尝试以下内容,但都没有成功
("^([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 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.
答案1
得分: 2
- `^` = 字符串的开头
- `\d{5}` = 5 个数字
- `\s` = 单个空格
- `[A-Z\s]*` = 任意字母和空格的序列,可能为空
- `[A-Z]` = 单个字母,所以不能以空格结束
- `$` = 字符串的结尾
英文:
^\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
答案2
得分: 1
不确定你正在寻找什么,但在JavaScript中,可以使用正则表达式来匹配一个5位数字后面跟任意数量的文本,包括空格。更新后的表达式如下:
/^\d{5} [a-zA-Z\s]*[a-zA-Z]$/
,这个表达式将匹配以5位数字开头,后跟一个空格,以字母结尾,中间可以包含任意字母和空格的组合。
^:
匹配字符串的开头\d{5}:
匹配任意5位数字:
匹配一个空格[a-zA-Z\s]*:
匹配任意字母和空格的组合[a-zA-Z]:
匹配字符串末尾的一个字母$:
匹配字符串的末尾
旧答案:
/\d{5}\s*.*/
/\d{5}
精确匹配五位数字(0-9),\s*
匹配任意数量的空格(空格、制表符等),.*
匹配五位数字后的任意文本(包括空格),可能成功匹配的示例:"12345","12345 ","12345abc","12345 abc","12345 abc def"。这也会匹配字符串"99999 AAA AAA"。
你的脚本 ("^([0-9]{5}).([A-Z]+)$")
存在什么问题:
- 括号通常用作“捕获组”,简单来说,它们用于通过分组正则表达式的部分来捕获匹配。在你的代码中,括号在表达式中间的点
.
周围起到了字面字符的作用。 - 修复: 应删除括号中间的点。
- 其次,表达式中间的点
.
只匹配单个字符,包括空格和其他不是大写字母的字符。 - 修复: 用字符类替换点,以匹配任意数量的空格和任何大写字母。
正确解决方案(在使空格强制性之后进行了更新):/^\d{5}\s+[A-Z\s]+$/i
旧答案:/^[0-9]{5}\s*[A-Z\s]+$/
英文:
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]+$/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论