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

huangapple go评论90阅读模式
英文:

convert Golang regex to JS regex

问题

我可以帮你将Golang的正则表达式(nameComponentRegexp)转换为JavaScript风格的正则表达式。

你所遇到的主要问题是:

  1. 如何在JavaScript中正确地实现“optional”和“repeated”。
  2. 我尝试从match(`(?:[._]|__|[-]*)`)复制,但它无法匹配单个句点或单个下划线。我在在线正则表达式测试器上尝试了一下。

以下是Golang的描述:

> nameComponentRegexp 限制注册表路径组件名称以至少一个字母或数字开头,后续部分可以由一个句点、一个或两个下划线和多个破折号分隔。

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

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

一些有效的示例:

  • a.a
  • a_a
  • a__a
  • a-a
  • a--a
  • a---a
英文:

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

答案1

得分: 1

看一下你是如何构建nameComponentRegexp的:你首先使用alphaNumericRegexp,然后匹配1个或0个出现的1个或多个separatorRegexp+alphaNumericRegexp序列。

optional() 的作用如下:

> // optional将表达式包装在一个非捕获组中,并使其可选。<br/>
> // 返回匹配结果<br/>
><pre>func optional(res ...*regexp.Regexp) *regexp.Regexp {
> return match(group(expression(res...)).String() + ?)
>}</pre>

repeated() 的作用如下:

>// repeated将正则表达式包装在一个非捕获组中,以获得一个或多个匹配项。<br/>
>// 返回匹配结果<br/>
><pre>func repeated(res ...*regexp.Regexp) *regexp.Regexp {
> return match(group(expression(res...)).String() + +)
>}</pre>

因此,你需要的是

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

参见正则表达式演示

详细说明

  • ^ - 字符串的开始
  • [a-z0-9]+ - 1个或多个字母数字符号
  • (?:(?:[._]|__|-*)[a-z0-9]+)* - 零个或多个序列:
  • (?:[._]|__|-*) - 一个.___或0个连字符
  • [a-z0-9]+- 1个或多个字母数字符号

如果你想禁止像aaaa这样的字符串,你需要将模式中的所有*(2次出现)替换为+演示)。

JS演示:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

英文:

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<br/>
> // production optional.<br/>
><pre>func optional(res ...*regexp.Regexp) *regexp.Regexp {
> return match(group(expression(res...)).String() + ?)
>}</pre>

repeated() does this:

>// repeated wraps the regexp in a non-capturing group to get one or more<br/>
>// matches.<br/>
><pre>func repeated(res ...*regexp.Regexp) *regexp.Regexp {
> return match(group(expression(res...)).String() + +)
>}</pre>

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

huangapple
  • 本文由 发表于 2017年7月24日 11:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/45272267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定