dqyin0101
2017-07-24 03:39
浏览 205

将Golang正则表达式转换为JS正则表达式

I have a regex from Golang (nameComponentRegexp). How can I convert it to JavaScript style regex?

The main blocking problem for me:

  1. How can I do optional and repeated in JavaScript correctly
  2. I tried copy from match(`(?:[._]|__|[-]*)`) but it cannot match single period or single underscore. I tried it from online regex tester.

The description from Golang:

nameComponentRegexp restricts registry path component names to start with at least one letter or number, with following parts able to be separated by one period, one or two underscore and multiple dashes.

alphaNumericRegexp = match(`[a-z0-9]+`)
separatorRegexp = match(`(?:[._]|__|[-]*)`)

nameComponentRegexp = expression(
    alphaNumericRegexp,
    optional(repeated(separatorRegexp, alphaNumericRegexp)))

Some valid example:

  • a.a
  • a_a
  • a__a
  • a-a
  • a--a
  • a---a

图片转代码服务由CSDN问答提供 功能建议

我有一个来自Golang的正则表达式(nameComponentRegexp)。 如何将其转换为JavaScript样式的正则表达式?

对我来说主要的阻塞问题:

  1. 我该如何可选 正确地在JavaScript中重复
  2. 我尝试从 match(`(?:[._] | __ | [-] *)`)复制 不能匹配单个句点或单个下划线。 我从在线正则表达式测试仪上尝试过。

    来自Golang的描述:

    nameComponentRegexp限制了注册表路径 组件名称,请以至少一个字母或数字开头,以下部分可以用一个句点,一个或两个下划线和多个破折号分隔。\ p < pre> alphaNumericRegexp = match(`[a-z0-9] +`) separatorRegexp = match(`(?:[._] | __ | [-] *)`) nameComponentRegexp = expression( alphaNumericRegexp, 可选(重复(separatorRegexp,alphaNumericRegexp)))

    一些有效的示例:

    • AA
    • A_A
    • a__a
    • AA
    • AA
    • a --- a
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dps123456789 2017-07-24 06:41
    已采纳

    See how you build the nameComponentRegexp: you start with alphaNumericRegexp and then match 1 or 0 occurrences of 1 or more sequences of separatorRegexp+alphaNumericRegexp.

    optional() does the following:

    // optional wraps the expression in a non-capturing group and makes the
    // production optional.

    func optional(res ...*regexp.Regexp) *regexp.Regexp {
      return match(group(expression(res...)).String() + `?`)
    }

    repeated() does this:

    // repeated wraps the regexp in a non-capturing group to get one or more
    // matches.

    func repeated(res ...*regexp.Regexp) *regexp.Regexp {
      return match(group(expression(res...)).String() + `+`)
    }

    Thus, what you need is

    /^[a-z0-9]+(?:(?:[._]|__|-*)[a-z0-9]+)*$/
    

    See the regex demo

    Details:

    • ^ - start of string
    • [a-z0-9]+ - 1 or more alphanumeric symbols
    • (?:(?:[._]|__|-*)[a-z0-9]+)* - zero or more sequences of:
      • (?:[._]|__|-*) - a ., _, __, or 0+ hyphens
      • [a-z0-9]+- 1 or more alphanumeric symbols

    If you want to disallow strings like aaaa, you need to replace all * in the pattern (2 occurrences) with + (demo).

    JS demo:

    var ss = ['a.a','a_a','a__a','a-a','a--a','a---a'];
    var rx = /^[a-z0-9]+(?:(?:[._]|__|-*)[a-z0-9]+)*$/;
    for (var s of ss) {
     console.log(s,"=>", rx.test(s));
    }

    </div>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题