英文:
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。删除其他文本。
所以应该是这样的:
- 获取第三次出现的 $。
- 删除其余文本:$command this my first text $ this is my second text $ 和 $ this is my fourth text
- 获取 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.
- Get the third occurrence of $.
- Remove the rest of the text: $command this my first text $ this is my second text $ and $ this is my fourth text
- 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 = '$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);
<!-- 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 = "$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$])/;
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 = "$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])
<!-- 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 = '$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);
<!-- end snippet -->
The library should accept positive lookahead and lookbehind so you could try this:
(?<=((?<=$).*?$){2}) this is my.*?(?=$)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论