我看了很多东西,所有的东西都指向在正则表达式中添加一个空格,但它对我不起作用,你能解释一下为什么吗?我怎么才能让它起作用呢?
现在我使用的表达式是this,它是用JavaScript编写的,这意味着它允许用户使用\s来表示空格,但在dart中它只是一个空格““。
RegExp(r'''^[\w\'\’\"_,.()&!*|:/\\–%-]+(?:\s[\w\'\’\"_,.()&!*|:/\\–%-]+)*?\s?$''');
我尝试只接受字符串开头的字母数字字符,然后在字符或单词之后只允许一个空格,但其他字符可以超过这个空格。
这就是我在代码中使用它的方式。
inputFormatters: [
RegexInputFormatter(featureRegex),
],
class RegexInputFormatter extends TextInputFormatter {
final RegExp regex;
RegexInputFormatter(this.regex);
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
final String newText = newValue.text;
final String filteredText = _getFilteredText(newText);
if (newText != filteredText) {
final int selectionIndex = newValue.selection.end - (newText.length - filteredText.length);
return TextEditingValue(
text: filteredText,
selection: TextSelection.collapsed(offset: selectionIndex),
);
}
return newValue;
}
比如说,
接受的字符串:
- 亚洲人124
- sfaasf(space)1221
- afasf(空格).124,(空格)aaf
- 1242dsdd(space)sddl,.!
- 1242dsdd(space)sddl,,...!!!!
不可接受的字符串:
- (space)(space)asfa
- (space)
- 联系我们
- aas(空间),阿萨(空间)作为
2条答案
按热度按时间f45qwnt81#
试试这个正则表达式
tktrz96b2#
试试这个代码