测试所有双引号对之间的/字符

Test for / character in between all pairs of opening and closing double quotes

本文关键字:之间 字符 测试      更新时间:2023-09-26

我正在测试/字符是否在JavaScript字符串的双引号内。例如,"hello/world"将匹配,而hello/world/"How are you"将失败,因为它在双引号之外。我一直在四处寻找,却没有找到答案。

此外,"hello"world/how"are you"应该失败,因为从左到右确定,开头和结尾的双引号没有封装/。重申一下,我想匹配所有内/内/内闭合和打开双引号之间的实例,这与regex的Alternative不同:匹配所有不在引号内的实例

我认为你可以使用这个:

/^((('"[^"'/]*'")'/?)|([^"'/]*'/?))+$/igm

/^('"[^"'/]*'"|[^"'/]*'/?)+$/im

[Regex Demo]

str = 'hello/world"how are you"';
q1 = str.indexOf('"');
q2 = str.lastindexof('"');
slsh = str.indexOf("/");
if( slsh > q1 && slsh <q2){
    dosomething;
}

如果不使用正则表达式,算法将类似于。。。请测试一下自己!

 Startquotehit = false
    insidequote = false
    Gotaslash = false
    Wantedresult = false
    For x to.. String.length
    {
    If x == quote
    {
     Startquotehit = true;
     Insidequote = !insidequote;
     If gotaslash == true && insidequote
         Wantedresult = true;
    }     
    If x == slash
    {
      If startquotehit
         Gotaslash = true;
    }
 }

Wantedresult会告诉你斜杠是否被"引号内"引用,以覆盖你的错误场景"……"…/…"…">

/".*'/.*"/.test(your_string)

请参阅JavaScript正则表达式的测试方法。