英文:
Regular Expression does not work properly in Adonis Js
问题
I have tried several regex found on SO and as google resulted.
我尝试了在SO和Google上找到的多个正则表达式。
I am trying to use regex
as validating rule in adonis js
as
我正在尝试在adonis js
中将regex
用作验证规则,如下:
first_name: 'required|regex:[a-zA-z]+([\s][a-zA-Z]+)*$'
我的目标是实现这一点;
只接受单词之间的单个空格,
regex which accepts only single space between words,
正则表达式只接受单词之间的单个空格,
John Doe - True //(single space between words and no space before and after)
John Doe - True //(single space between and one space before)
John doe - true //(single space between and one space after)
jhon doe - false //(two spaces between words. )
John3 Doe - false
3John Doe - False
John Doe - True //(单词之间有单个空格,前后都没有空格)
John Doe - True //(单词之间有一个空格,前面有一个空格)
John doe - true //(单词之间有一个空格,后面有一个空格)
jhon doe - false //(单词之间有两个空格。)
John3 Doe - false
3John Doe - False
I have tried several regex which works on online regex checkers but does not responds properly in adonis
我尝试了在在线正则表达式检查器上有效的多个正则表达式,但在adonis中没有正确响应
here in one https://regex101.com/r/pN8xL5/312
在这里 https://regex101.com/r/pN8xL5/312 有一个例子。
英文:
> I have tried several regex found on SO and as google resulted.
I am trying to use regex
as validating rule in adonis js
as
first_name: 'required|regex:[a-zA-z]+([\s][a-zA-Z]+)*$'
My goal is to achieve this;
regex which accepts only single space between words,
John Doe - True //(single space between words and no space before and after)
John Doe - True //(single space between and one space before)
John doe - true //(single space between and one space after)
jhon doe - false //(two spaces between words. )
John3 Doe - false
3John Doe - False
> I have tried several regex which works on online regex checkers but
> does not responds properly in adonis
here in one https://regex101.com/r/pN8xL5/312
答案1
得分: 2
I don't know Adonis, but it looks like it tried to match the whole string.
Using regex101 web site, I came up with the following regex, with g and m options:
^ *([a-zA-Z]+) ([a-zA-Z]+) *
It matches:
^
- start of row,*
- an initial sequence of spaces (if any),([a-zA-Z]+)
- a non-empty sequence of letters (the first capturing group),([a-zA-Z]+)
- another non-empty sequence of letters (the second capturing group),*
- a trailing sequence of spaces (if any).
In regex101 it matches first 3 input rows from your sample, just as you want.
As I suppose, your regex should check only a single string,
so in the final version of regex:
- remove leading ^ and trailing $,
- remove regex options,
so your regex to be used in Adonis should be:
*([a-zA-Z]+) ([a-zA-Z]+) *
英文:
I don't know Adonis, but it looks like it tried to match the whole string.
Using regex101 web site, I came up with the following regex, with g and
m options:
^ *([a-zA-Z]+) ([a-zA-Z]+) *$
It matches:
^
- start of row,*
- an initial sequence of spaces (if any),([a-zA-Z]+)
- a non-empty sequence of letters (the first capturing group),([a-zA-Z]+)
- another non-empty sequence of letters (the second capturing group),*
- a trailing sequence of spaces (if any).$
- end of row.
In regex101 it matches first 3 input rows from your sample, just as you want.
As I suppose, your regex should check only a single string,
so in the final version of regex:
- remove leading ^ and trailing $,
- remove regex options,
so your regex to be used in Adonis should be:
*([a-zA-Z]+) ([a-zA-Z]+) *
答案2
得分: -1
请查看这个答案:forum.adonisjs.com/t/rule-not-accepting-regex/
你需要使用 rule()
来处理正则表达式。类似这样:
const { validate, rule } = use("Validator");
const rules = {
first_name: [
rule(
"required"
),
rule(
"regex",
[a-zA-z]+([\s][a-zA-Z]+)*$
),
...
]
};
英文:
Please see this answer : forum.adonisjs.com/t/rule-not-accepting-regex/
You need to use rule()
for regex. Something like :
const { validate, rule } = use("Validator");
const rules = {
first_name: [
rule(
"required"
),
rule(
"regex",
[a-zA-z]+([\s][a-zA-Z]+)*$
),
...
]
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论