JavaScript正则表达式给出了误报

时间:2015-05-18 18:23:55

标签: javascript regex

我正在检查字符串的第一个字符,以查找来自多行文本框的任何非字母数字字符。如果我传入这个字符串,我会得到一些点击。

ATest
BTest2
CAnother1
DThen some
2 More to go
FAny other some-day

这是javascript代码:

function CheckFirstAlphaNum(textToTest, valueLabel, pattern, warningMessage)     
{
    var textValues = textToTest.split("\n");
    pattern = pattern || /([A-Za-z0-9]+)/gm;
    warningMessage = warningMessage || "The first character of a value in '" + valueLabel + "' has an invalid character. Please consider revising it."

    var goodText = true;
    for (var i = 0; i < textValues.length; ++i) {
        //alert(textValues[i]);
        var firstChar = textValues[i].substring(0, 1);
        if (!pattern.test(firstChar)) {
            alert(firstChar);
            alert(pattern);
            goodText = false;
        }
    }
    if (goodText != true) { alert(warningMessage); }
    return goodText;
}

任何想法为什么?它在B,D和F上失败,所以看起来像是其他每一行。

3 个答案:

答案 0 :(得分:1)

可能是因为你的正则表达式不正确。

我不确定最后/gm个开关是做什么的,但请尝试:

/^[A-Za-z0-9]/

从您的原始正则表达式中,需要注意几点:

  1. ^匹配字符串的开头
  2. 如果你只关心一个角色,
  3. +没用。

答案 1 :(得分:1)

此处的问题是您在正则表达式中使用g(全局)修饰符。

在JS控制台中运行此代码,以检查test()是否交替truefalse

r = /([A-Za-z0-9]+)/g
r.test('a')
> true
r.test('a')
> false
r.test('a')
> true
...

删除g修饰符会导致正则表达式始终对匹配的字符串返回true。由于您只使用多线修饰符m测试一个字符,因此也不需要+(一个或多个匹配)。

答案 2 :(得分:0)

您的文字很可能包含Windows换行符using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Gms.Maps.Model; using Android.Gms.Maps; using Android.Support.V4.Graphics; using Android.Support.V7.AppCompat; namespace CyclingAndroid { [Activity (Label = "MapActivity")] public class MapActivity : Activity { protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); var mapFragment = new MapFragment (); FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction(); fragmentTx.Add (Resource.Id.mainlayout, mapFragment); fragmentTx.Commit (); SetContentView (Resource.Layout.Map_View); } protected override void OnResume () { base.OnResume (); } } } ,而不仅仅是Java.Lang.NoSuchFieldError: com.google.android.gms.R$styleable.MapAttrs_liteMode at at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068> at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:896 at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:4253 at CyclingAndroid.MapActivity.OnCreate (Android.OS.Bundle) [0x00009] in d:\Biker Finder 13-5-2015\BikerFinder 12-5-2015\VR2\Cycling Mobile\CyclingAndroid\Activity\MapActivity.cs:29 at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2179 at at (wrapper dynamic-method) object.8727fe2f-d554-4ad5-a0a8-e6e04355ca49 (intptr,intptr,intptr) <IL 0x00017, 0x00043> at --- End of managed exception stack trace --- at java.lang.NoSuchFieldError: com.google.android.gms.R$styleable.MapAttrs_liteMode at at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) at at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source) at at android.app.Activity.onCreateView(Activity.java:4729) at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) at at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274) at at android.app.Activity.setContentView(Activity.java:1881) at at cyclingandroid.MapActivity.n_onCreate(Native Method) at at cyclingandroid.MapActivity.onCreate(MapActivity.java:29) at at android.app.Activity.performCreate(Activity.java:5117) at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081) at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150) at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236) at at android.app.ActivityThread.access$600(ActivityThread.java:145) at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) at at android.os.Handler.dispatchMessage(Handler.java:99) at at android.os.Looper.loop(Looper.java:137) at at android.app.ActivityThread.main(ActivityThread.java:5099) at at java.lang.reflect.Method.invokeNative(Native Method) at at java.lang.reflect.Method.invoke(Method.java:511) at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803) at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570) at at dalvik.system.NativeStart.main(Native Method) 。因此,在\r\n处拆分将在每个其他数组索引上生成\n个条目。

那些\n将“触发”正则表达式.test()方法,因为它不是字母数字。

试试这个

\r
相关问题