匹配 JavaScript 中的任何内容直到模式。

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

Javascript match anything until pattern

问题

Here are the translated parts of your content:

  • Given example-123, I need to extract just example. Also example could be: example-example-345, in which case I need example-example. This is the pattern I'm looking for:

    > str.match('-[0-9]{1,}$')[0]
    '-123'

  • I tried:

    str.match(/(.*)-[0-9]{1,}$/)
    'example-123'

and

str.match(/(?s).*)-[0-9]{1,}$/)[0]
Uncaught SyntaxError: Invalid regular expression: /(?s).*)-[0-9]{1,}$/: Invalid group

and

str.match('[^-[0-9]{1,}$]')
null

and

str.match('(.*)[^-[0-9]{1,}$]')
null

and

str.match('/.*/[^-[0-9]{1,}$]')
null

and

str.match('.*^(-[0-9]{1,}$)')
null

... the list goes on

英文:

Given example-123, I need to extract just example. Also example could be: example-example-345, in which case I need example-example. This is the pattern I'm looking for:

> str.match('-[0-9]{1,}$')[0]
'-123'

I tried:

str.match(/(.*)-[0-9]{1,}$/)
'example-123'

and

str.match(/(?s).*)-[0-9]{1,}$/)[0]
Uncaught SyntaxError: Invalid regular expression: /(?s).*)-[0-9]{1,}$/: Invalid group

and

str.match('[^-[0-9]{1,}$]')
null

and

str.match('(.*)[^-[0-9]{1,}$]')
null

and

str.match('/.*/[^-[0-9]{1,}$]')
null

and

str.match('.*^(-[0-9]{1,}$)')
null

... the list goes on

答案1

得分: 1

你可以使用 正向预查 来实现这个:

let str = 'example-example-123';
console.log(str.match(/.+(?=-123)/)[0]);

str = "example-123";
console.log(str.match(/.+(?=-123)/)[0]);

str = "example-example-example-123-something-after";
console.log(str.match(/.+(?=-123)/)[0]);
英文:

You can use a positive lookahead for that:

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

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

let str = &#39;example-example-123&#39;;
console.log(str.match(/.+(?=-123)/)[0]);

str = &quot;example-123&quot;;
console.log(str.match(/.+(?=-123)/)[0]);

str = &quot;example-example-example-123-something-after&quot;;
console.log(str.match(/.+(?=-123)/)[0]);

<!-- end snippet -->

答案2

得分: 1

匹配直到连字符后面的一切,然后跟随数字!使用[1]将仅获取第一部分!

英文:

Try this:

str.match(/^(.+)-\d+$/)[1]

matching everything till a hyphen followed by numbers!

And with [1] it will get the first part only!

答案3

得分: 1

你可以使用 *? 来匹配前一个标记零次或多次,尽可能少地匹配,根据需要展开(懒惰匹配,非贪婪匹配)

const str = 'example-example-123';

console.log(str.match(/(.*?)-\d/)[1]);
英文:

You can use *? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy, none greedy match)

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

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

const str = &#39;example-example-123&#39;;

console.log(str.match(/(.*?)-\d/)[1]);

<!-- end snippet -->

答案4

得分: 0

可以匹配正则表达式

^([a-z]+)(?:-)*(?=-)

示例

我假设第一个连字符之前的单词只能包含小写字母。当然,这个假设可能是错误的,如果需要匹配一个字母后面跟着零个或多个字母和数字,可以将 ([a-z]+) 更改为 ([a-zA-Z][a-zA-Z0-9]*)


如链接所示,以下由派对帽标记的文本被匹配。

example-123
^^^^^^^

example-example-345
^^^^^^^^^^^^^^^

dog-dog-dog-123
^^^^^^^^^^^

dog-dog-cat-123
^^^^^^^

dog-cat-cat 123
^^^^^^^^^^^

dog-dog123
^^^

dog

最后一个示例中没有匹配项。


正则表达式可以分解如下。

^         匹配字符串的开头
(         开始捕获组 1
  [a-z]+  匹配一个或多个小写字母
)         结束捕获组 1
(?:       开始非捕获组
  -     匹配连字符后面跟着捕获组 1 的内容
)*        结束非捕获组,可以执行零次或多次
(?=       开始正向预查
  -       匹配连字符
)         结束正向预查

注意,正向预查 (?=-) 强制匹配后面跟着一个连字符,但该连字符不是返回的匹配的一部分。

英文:

You can match the regular expression

^([a-z]+)(?:-)*(?=-)

Demo

I have assumed the word preceding the first hyphen must contain only lowercase letters. That assumption may be faulty, of course, in which case ([a-z]+) would have to be changed accordingly. To match a letter followed by zero or more letters and numbers, for example, it would be ([a-zA-Z][a-zA-Z0-9]*).


As shown at the link, the text indicated below by the party hats was matched.

example-123
^^^^^^^

example-example-345
^^^^^^^^^^^^^^^

dog-dog-dog-123
^^^^^^^^^^^

dog-dog-cat-123
^^^^^^^

dog-cat-cat 123
^^^^^^^^^^^

dog-dog123
^^^

dog

There is no match in the last example.


The regular expression can be broken down as follows.

^         Match beginning of the string
(         Begin capture group 1
  [a-z]+  Match one or more lowercase letters
)         End capture group 1
(?:       Begin non-capture group
  -     Match a hyphen followed by the contents of capture group 1
)*        End the non-capture group and execute it zero or more times
(?=       Begin a positive lookahead
  -       Match a hyphen
)         End the positive lookahead

Note that the positive lookahead (?=-) forces the match to be followed by a hyphen but that hyphen is not part of the match that is returned.

huangapple
  • 本文由 发表于 2023年3月8日 16:19:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670699.html
匿名

发表评论

匿名网友

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

确定