确定char是否在正则表达式中

时间:2018-01-10 15:07:29

标签: javascript regex

我试图使用Quill做一个自定义提及系统,我需要弄清楚如何判断给定的字符是否在字母表中。

示例:

我有这个字符串:Hello my @name is Luis和这个函数,它取光标的位置并评估单词以检查它是否包含@

CheckWord = function(quill, start){
    let at = false, c_char;
    for(var i = start; i > 0; --i){
        c_char = quill.getText(i, 1);
        if (c_char == '@') {
            if (quill.getText(i-1, 1) == ' ' || quill.getText(i-1, 1) == '') {
                at = true;
                break;
            }
        }
        if (c_char == ' ') {
            at = false;
            break;
        }
    }
    return at;
}

一切正常但

 if (c_char == ' ') {
     at = false;
     break;
 }

我需要验证不是字母字符(A,B,C,D,等等)

我知道使用像这样的正则表达式/^[a-zA-Z]+$/我可以实现我想要的但我不知道如何实现它只是为了检查给定的字母是否有效。

1 个答案:

答案 0 :(得分:4)

您可以使用RegExp.prototype.test()来测试字符是否为字母(a-z或A-Z),如果您想匹配字母以外的其他字符,请忽略此项:

using Microsoft.ProjectOxford.Face;
using Microsoft.ProjectOxford.Common;
using Microsoft.ProjectOxford.Face.Contract;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FaceDetection.FaceDetect
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Face Detect";
            Start();
        }
        static async Task Stop()
        {
            await Close();
        }
        private static Task Close()
        {
            return Task.Run(() =>
            {
                Environment.Exit(0);
            });
        }

        static async Task ReStart(string _reason = "")
        {
            Console.WriteLine(_reason + "To restart the process press 'R'. To exit press 'X'");
            var _response = Console.ReadLine();
            if (_response == "r" || _response == "R")
                await Start();
            else
                await Stop();
        }
        static async Task Start()
        {
            Console.Clear();
            Console.WriteLine("Enter Folder Path");
            string imageFolderPath = Console.ReadLine();
            if (!Directory.Exists(imageFolderPath))
            {
                await ReStart("Folder does not exist! ");
            }
            else
            {
                await SaveFiles(imageFolderPath);
            }
        }
        static async Task SaveFiles(string imageFolderPath)
        {
            try
            {
                DirectoryInfo dInfo = new DirectoryInfo(imageFolderPath);
                string[] extensions = new[] { ".jpg", ".jpeg" };
                FileInfo[] files = dInfo.GetFiles()
                .Where(f => extensions.Contains(f.Extension.ToLower()))
                .ToArray();
                if (files.Length == 0)
                    await ReStart("No files found in the specified folder! ");
                else
                {
                    string subscriptionKey = "ADSFASDFASDFASDFASDFASDFASDF";
                    if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["subscriptionKey"]))
                        subscriptionKey = ConfigurationManager.AppSettings["subscriptionKey"].ToString();

                    //var stringFaceAttributeType = new List<FaceAttributeType> { FaceAttributeType.Smile, FaceAttributeType.Glasses, FaceAttributeType.Gender, FaceAttributeType.Age };
                    //IEnumerable<FaceAttributeType> returnFaceAttributes = stringFaceAttributeType;

                    IFaceServiceClient faceServiceClient = new FaceServiceClient(subscriptionKey);
                    foreach (FileInfo file in files)
                    {
                        try
                        {
                            using (FileStream fileStream = File.OpenRead(imageFolderPath + "\\" + file.Name))
                            {
                                MemoryStream memStream = new MemoryStream();
                                memStream.SetLength(fileStream.Length);
                                fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

                                //Used following commented code to make sure MemoryStream is not corrupted.
                                //FileStream _file = new FileStream(imageFolderPath + "\\test.jpg", FileMode.Create, FileAccess.Write);
                                //memStream.WriteTo(_file);
                                //_file.Close();
                                //memStream.Close();

                                try
                                {
                                    //This line never returns a result. The execution terminates without any exception/error.
                                    var faces = await faceServiceClient.DetectAsync(memStream, true, true);

                                    if (faces != null)
                                    {
                                        foreach (var face in faces)
                                        {
                                            var rect = face.FaceRectangle;
                                            var landmarks = face.FaceLandmarks;
                                        }
                                    }
                                    else
                                        Console.WriteLine("No face found in image: " + file.FullName);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Error");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("There was an error!");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("There was an error!");
            }
            await ReStart();
        }
    }
}

正则表达式中的if (/[a-z]/i.test(c_char)) { at = false; break; } 表示不区分大小写的搜索(将i视为i nsensitive),即它检查小写和大写字母。由于i是单个字符,因此您不需要正则表达式中的c_char(输入开头)和^(输入结束)字符。