代码之家  ›  专栏  ›  技术社区  ›  Huy Le

角度6输入函数,用逗号和空格检测名字和姓氏

  •  -2
  • Huy Le  · 技术社区  · 6 年前

    案例1:“word”-空格就是物质,把字符串当作“first last”

    以上情况将被视为触发发送API查询的成功案例。我做了一些研究,我不能为此构建一个定制的角度管道,因为管道是用来显示数据的,而不是用来接收数据的。所以我要建立一个解析函数来测试这个字符串。

    我现在对案例1和案例3的方法是重叠的。

    transformInput(inputValue) {
        const case1 = /(([A-Za-z])+(\s)+[A-Za-z]*)/;
        const case2 = //NOT SURE;
        const case3 = /(^([A-Za-z]*)+\s)/;
    
        if (case1.test(inputValue) {
        console.log('case1')
        } else if (case2.test(inputValue)) {
        console.log('case2')
        } else if...
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   emsimpson92    6 年前

    这应该起作用: \w+,?\s?\w*

    如果你想更具体,你可以用 [a-zA-Z] 而不是 \w

    Demo