获取字符串中第n个”$”分隔符之后的特定部分,js

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

Regex get specific part of string after the nth "$" delimiter, js

问题

这是我的discord命令。

> $command this my first text $ this is my second text $ this is my
> third text $ this is my fourth text

我需要获得第三部分文本,输出应该像这样:this is my third text。删除其他文本。

所以应该是这样的:

  1. 获取第三次出现的 $
  2. 删除其余文本:$command this my first text $ this is my second text $$ this is my fourth text
  3. 获取 this is my third text 部分。

如何在正则表达式中实现这个?我在这里搜索了一些帖子,有些人说要使用捕获组之类的东西,但我无法使其工作。

(?:$){2}(\s.*)
英文:

So I have this for a discord command.

> $command this my first text $ this is my second text $ this is my
> third text $ this is my fourth text

I need to get for example the third text part, and it should output like this: this is my third text. Removing the other text.

So it should be something like this.

  1. Get the third occurrence of $.
  2. Remove the rest of the text: $command this my first text $ this is my second text $ and $ this is my fourth text
  3. Get the this is my third text part.

How do you do that in regex? I searched the threads here and some said to use something like a capture group, but I couldn't make it work.

(?:$){2}(\s.*)

答案1

得分: 1

你可以使用 \$[^$]+ 来匹配 $ 后跟随其他字符的部分。

let s = '$command this my first text $ this is my second text $ this is my third text $ this is my fourth text';
let res = s.match(/(?:$[^$]+){2}$([^$]+)/)[1].trim();
console.log(res);
英文:

You can use \$[^$]+ to match $ followed by other characters.

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

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

let s = &#39;$command this my first text $ this is my second text $ this is my third text $ this is my fourth text&#39;;
let res = s.match(/(?:$[^$]+){2}$([^$]+)/)[1].trim();
console.log(res);

<!-- end snippet -->

答案2

得分: 1

使用捕获组:

^(?:[^$]*$){3}\s*([^$]*[^\s$])

解释

  • ^ 字符串的开头
  • (?: 非捕获组,整体重复
    • [^$]*\$ 匹配除 $ 之外的可选字符,然后匹配 $
  • ){3} 关闭非捕获组,重复3次
  • \s* 匹配可选的空白字符
  • ( 捕获 组 1
    • [^$]* 匹配除 $ 之外的可选字符
    • [^\s$] 匹配除 $ 之外的非空白字符
  • ) 关闭组 1

查看 正则表达式演示

使用前行断言(如果支持)只匹配:

const s = "$command this my first text $ this is my second text $ this is my third text $ this is my fourth text";
const regex = /(?<=^(?:[^$]*$){3}\s*)[^\s$](?:[^$]*[^\s$])?/;
const m = s.match(regex);
if (m) console.log(m[0]);
英文:

Using a capture group:

^(?:[^$]*$){3}\s*([^$]*[^\s$])

Explanation

  • ^ Start of string
  • (?: Non capture group to repeat as a whole part
    • [^$]*\$ Match optional chars other than $ and then match $
  • ){3} Close the non capture group and repeat it 3 times
  • \s* Match optional whitespace chars
  • ( Capture group 1
    • [^$]* Match optional chars other than $
    • [^\s$] Match a non whitespace character other than $
  • ) Close group 1

See a regex demo.

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

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

const s = &quot;$command this my first text $ this is my second text $ this is my third text $ this is my fourth text&quot;;
const regex = /^(?:[^$]*$){3}\s*([^$]*[^\s$])/;
const m = s.match(regex);
if (m) console.log(m[1])

<!-- end snippet -->

Using a lookbehind assertion if that is supported to get a match only:

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

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

const s = &quot;$command this my first text $ this is my second text $ this is my third text $ this is my fourth text&quot;;
const regex = /(?&lt;=^(?:[^$]*$){3}\s*)[^\s$](?:[^$]*[^\s$])?/;
const m = s.match(regex);
if (m) console.log(m[0])

<!-- end snippet -->

答案3

得分: 0

let s = '$command this my first text $ this is my second text $ this is my third text $ this is my fourth text';
let res = s.match(/(?<\=((?<=$).*?$){2}) this is my.*?(?=$)/)[0];
console.log(res);
英文:

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

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

let s = &#39;$command this my first text $ this is my second text $ this is my third text $ this is my fourth text&#39;;
let res = s.match(/(?&lt;=((?&lt;=$).*?$){2}) this is my.*?(?=$)/)[0];
console.log(res);

<!-- end snippet -->

The library should accept positive lookahead and lookbehind so you could try this:

(?&lt;=((?&lt;=$).*?$){2}) this is my.*?(?=$)

huangapple
  • 本文由 发表于 2023年3月9日 22:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686171.html
匿名

发表评论

匿名网友

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

确定