Anyone have a regex to allow alphanumerics and -_",' as well as white spaces?
任何人都有正则表达式允许字母数字和-_“,”以及空格?
2 个解决方案
#1
3
Try this one:
试试这个:
/^[A-Za-z0-9-_",'\s]+$/
#2
0
I have read quite a lot on the \s flag within regex, I see it handed out like candy all over * however it would appear that the \s flag matches all on metacharacters and also skips line breaks.
我在正则表达式中的标志上已经阅读了相当多的内容,我看到它像糖果一样在*上分发,但是看起来\ s标志匹配所有元字符并且还跳过换行符。
This will allow ignoring filtered user input for all kinds of characters you do not want in your software, website or database.
这将允许忽略您在软件,网站或数据库中不需要的各种字符的过滤用户输入。
The \s flag also skips new line breaks leaving filtered code vulnerable to inclusion so use this method at your own risk otherwise happy hacking lol...
\ s标志还会跳过新的换行符,使得过滤后的代码容易被包含在内,因此请自行承担使用此方法的风险,否则请高兴哈哈哈哈哈...
You may consider something like this: /[^\p{Xan}]++$/D
你可以考虑这样的事情:/ [^ \ p {Xan}] ++ $ / D.
-
\p{Xan}
matches all unicode alphabet letters and numbers, if this
doesn't allow all alphabet white space then I am unsure how to safly match these for a filter.\ p {Xan}匹配所有unicode字母和数字,如果这不允许所有字母表空格,那么我不确定如何安全地匹配这些过滤器。
-
++
makes use of the possessive quantifier that can help optimize the match++利用占有量词,可以帮助优化匹配
-
$/D
causes the regex to terminate at the end of the string and not skip over any characters before a line break$ / D导致正则表达式终止于字符串的结尾,并且不会在换行符之前跳过任何字符
\s flag:
Ref: http://php.net/manual/en/reference.pcre.pattern.modifiers.php
s (PCRE_DOTALL) If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
s(PCRE_DOTALL)如果设置了此修饰符,则模式中的点元字符将匹配所有字符,包括换行符。没有它,排除了换行符。此修饰符等效于Perl的/ s修饰符。诸如[^ a]之类的否定类始终匹配换行符,与此修饰符的设置无关。
Metacharacters:
Ref: http://en.wikipedia.org/wiki/Metacharacter
A metacharacter is a character that has a special meaning (instead of a literal meaning) to a computer program, such as a shell interpreter or a regular expression engine.
元字符是一种对计算机程序具有特殊含义(而不是字面含义)的字符,例如shell解释器或正则表达式引擎。
In regular expressions, there are 11 metacharacters that must always be preceded by a backslash, \, to be used inside of the expression:
在正则表达式中,有11个元字符必须始终以反斜杠开头,\才能在表达式中使用:
The opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ).[1]
开口方括号[,反斜杠\,插入符号^,美元符号$,句点或点。,竖线或竖线符号|,问号?,星号或星号*,加号+,打开圆括号(和闭合圆括号)。[1]
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1+1=2. Otherwise, the plus sign will have a special meaning.
如果要在正则表达式中使用任何这些字符作为文字,则需要使用反斜杠转义它们。如果要匹配1 + 1 = 2,正确的正则表达式是1 + 1 = 2。否则,加号将具有特殊含义。
#1
3
Try this one:
试试这个:
/^[A-Za-z0-9-_",'\s]+$/
#2
0
I have read quite a lot on the \s flag within regex, I see it handed out like candy all over * however it would appear that the \s flag matches all on metacharacters and also skips line breaks.
我在正则表达式中的标志上已经阅读了相当多的内容,我看到它像糖果一样在*上分发,但是看起来\ s标志匹配所有元字符并且还跳过换行符。
This will allow ignoring filtered user input for all kinds of characters you do not want in your software, website or database.
这将允许忽略您在软件,网站或数据库中不需要的各种字符的过滤用户输入。
The \s flag also skips new line breaks leaving filtered code vulnerable to inclusion so use this method at your own risk otherwise happy hacking lol...
\ s标志还会跳过新的换行符,使得过滤后的代码容易被包含在内,因此请自行承担使用此方法的风险,否则请高兴哈哈哈哈哈...
You may consider something like this: /[^\p{Xan}]++$/D
你可以考虑这样的事情:/ [^ \ p {Xan}] ++ $ / D.
-
\p{Xan}
matches all unicode alphabet letters and numbers, if this
doesn't allow all alphabet white space then I am unsure how to safly match these for a filter.\ p {Xan}匹配所有unicode字母和数字,如果这不允许所有字母表空格,那么我不确定如何安全地匹配这些过滤器。
-
++
makes use of the possessive quantifier that can help optimize the match++利用占有量词,可以帮助优化匹配
-
$/D
causes the regex to terminate at the end of the string and not skip over any characters before a line break$ / D导致正则表达式终止于字符串的结尾,并且不会在换行符之前跳过任何字符
\s flag:
Ref: http://php.net/manual/en/reference.pcre.pattern.modifiers.php
s (PCRE_DOTALL) If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
s(PCRE_DOTALL)如果设置了此修饰符,则模式中的点元字符将匹配所有字符,包括换行符。没有它,排除了换行符。此修饰符等效于Perl的/ s修饰符。诸如[^ a]之类的否定类始终匹配换行符,与此修饰符的设置无关。
Metacharacters:
Ref: http://en.wikipedia.org/wiki/Metacharacter
A metacharacter is a character that has a special meaning (instead of a literal meaning) to a computer program, such as a shell interpreter or a regular expression engine.
元字符是一种对计算机程序具有特殊含义(而不是字面含义)的字符,例如shell解释器或正则表达式引擎。
In regular expressions, there are 11 metacharacters that must always be preceded by a backslash, \, to be used inside of the expression:
在正则表达式中,有11个元字符必须始终以反斜杠开头,\才能在表达式中使用:
The opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ).[1]
开口方括号[,反斜杠\,插入符号^,美元符号$,句点或点。,竖线或竖线符号|,问号?,星号或星号*,加号+,打开圆括号(和闭合圆括号)。[1]
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1+1=2. Otherwise, the plus sign will have a special meaning.
如果要在正则表达式中使用任何这些字符作为文字,则需要使用反斜杠转义它们。如果要匹配1 + 1 = 2,正确的正则表达式是1 + 1 = 2。否则,加号将具有特殊含义。