代码之家  ›  专栏  ›  技术社区  ›  shafik

如何将字符串拆分为两个或多个单词?

  •  0
  • shafik  · 技术社区  · 7 年前

    "@bm5ja7qyd1ljuqcqb09@ == 'test2' and @bm5ja7qyd1ljuqcqb09@ != 'test1' or @wersf4vrdf1f5e@ == 'test2' and @sdfsd56fe6sdfs@ != 'test1'"
    

    我需要把它们分开 and or . 我想要这样的输出

    @bm5ja7qyd1ljuqcqb09@ == 'test2'
    @bm5ja7qyd1ljuqcqb09@ != 'test1'
    @wersf4vrdf1f5e@ == 'test2'
    @sdfsd56fe6sdfs@ != 'test1'
    

    我只能靠一个工作 .

    2 回复  |  直到 7 年前
        1
  •  4
  •   Mamun    7 年前

    | )

    var str = "@bm5ja7qyd1ljuqcqb09@ == 'test2' and @bm5ja7qyd1ljuqcqb09@ != 'test1' or @wersf4vrdf1f5e@ == 'test2' and @sdfsd56fe6sdfs@ != 'test1'"
    
    var strArr = str.split(/ and | or /g);
    console.log(strArr);

    你在评论中提到了另一个案例 将不在单引号内的单词分开:

    var str = "@bm5ja7qyd1ljuqcqb09@ == 'test2' and @bm5ja7qyd1ljuqcqb09@ != 'test1' or @wersf4vrdf1f5e@ == 'test2' and @sdfsd56fe6sdfs@ != 'test1' and @bm5ja7qyd1ljuqcqb09@ == 'test2 and or other value'"
    
    var strArr = str.split(/(?!\B'[^']*) and (?![^']*'\B)|(?!\B'[^']*) or (?![^']*'\B)/g);
    console.log(strArr);