正则表达式问题,用于修剪空白以验证名称输入

时间:2018-09-24 23:35:20

标签: javascript regex

检查输入的名称值是否包含名字和姓氏,并在两者之间留有空格。我使用以下正则表达式,但是当存在尾随空格时会遇到问题,它返回false。 如何忽略正则表达式中的尾随空格?

var nameArr = value.match(/^([^ ]{1,50})[, ]+(.{0,49}[^ ,])$/);

我解决拖尾空间的方法 var nameArr = value.trim()。match(/ ^([^] {1,50})[,] +(。{0,49} [^,])$ /); 除了使用修剪方法,还有其他方法吗?

2 个答案:

答案 0 :(得分:2)

您可以使用\s*来匹配零个或多个空格。在您的正则表达式的结尾和开头使用它,以允许前导和尾随空格:

var nameArr = value.match(/^\s*([^ ]{1,50})[, ]+(.{0,49}[^ ,])\s*$/);

答案 1 :(得分:1)

要在匹配对象中获得针对“ firstName lastName”或“ firstName,lastName”的修整匹配,请使用:

!ENTRY Activator 4 0 2018-09-24 16:46:55.345
!MESSAGE NullPointerException: null
!STACK 0
java.lang.NullPointerException
    at org.eclipse.m2e.jdt.internal.MavenClassifierManager.getClassifierClasspathProvider(MavenClassifierManager.java:131)
    at org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider.addProjectEntries(MavenRuntimeClasspathProvider.java:275)
    at org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider.addMavenClasspathEntries(MavenRuntimeClasspathProvider.java:183)
    at org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider.resolveClasspath0(MavenRuntimeClasspathProvider.java:148)
    at org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider$1.call(MavenRuntimeClasspathProvider.java:133)
    at org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider$1.call(MavenRuntimeClasspathProvider.java:1)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:151)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:99)
    at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:1355)
    at org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider.resolveClasspath(MavenRuntimeClasspathProvider.java:130)
    at org.eclipse.jdt.internal.launching.RuntimeClasspathProvider.resolveClasspath(RuntimeClasspathProvider.java:65)
    at org.eclipse.jdt.launching.JavaRuntime.resolveRuntimeClasspath(JavaRuntime.java:1719)
    at org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate.getClasspathAndModulepath(AbstractJavaLaunchConfigurationDelegate.java:459)
    at org.springframework.ide.eclipse.boot.launch.BootLaunchConfigurationDelegate.getClasspathAndModulepath(BootLaunchConfigurationDelegate.java:566)
    at org.eclipse.jdt.launching.JavaLaunchDelegate.launch(JavaLaunchDelegate.java:77)
    at org.springframework.ide.eclipse.boot.launch.AbstractBootLaunchConfigurationDelegate.launch(AbstractBootLaunchConfigurationDelegate.java:333)
    at org.springframework.ide.eclipse.boot.launch.BootLaunchConfigurationDelegate.launch(BootLaunchConfigurationDelegate.java:151)
    at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:859)
    at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:717)
    at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1039)
    at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1256)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:60)

如果要匹配“第一个,最后一个”大小写(“,”和“”的存在),请在正则表达式中使用[,] +:

var nameArr=value.match(/(?=\s*)(\w{1,50})[, ](\w{1,50})(?=\s*)/);

(?=模式)称为lookaheads,它们查找不匹配它们的指定模式,即模式应该在此处通过测试,但不会出现在匹配的对象中。因此给我们修剪的名称。