我看了一大堆东西,都指向在正则表达式中添加一个空格,但它对我不起作用,你能解释一下为什么以及如何才能让它起作用吗?
现在我使用的表达式是This,它是用Java编写的,这意味着它允许用户使用\S作为空格,但在省道中它只是一个空格"".
RegExp(r'''^[\w\'\’\"_,.()&!*|:/\\–%-]+(?:\s[\w\'\’\"_,.()&!*|:/\\–%-]+)*?\s?$''');
我try 在字符串的开头只接受字母数字字符,然后在字符或单词之后应该只允许一个空格,但其他字符可以超过这个空格.
这就是我在代码中使用它的方式.
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;
}
例如,
接受的字符串:
- Abfed124
- Sfaasf(空间)1221
- Afasf(空格).124,(空格)aaf
- 1242dsdd(空格)sddl,.!
- 1242dsdd(空格)sddl,,...!
不可接受的字符串:
- 嘿,嘿,嘿.
- ==同步,由长者更正==
- @ajsfas
- AAS(空间)(空间)、ASA(空间)(空间)AS