正则表达式在字符串中最后一次出现“#”后获取整个字符串

时间:2021-03-21 05:40:29

标签: javascript regex ecmascript-6

您好,我是使用 Regex 的新手。我有以下字符串 -

##H2\n\ncontent.\n\n## Headers\n\nTEST\n\n# H1 - Create the best documentation\n\nTEST\n\n# H2 - Create the best documentation\n\nTEST\n\n### H3 - Create the best documentation\n\nTEST\n\n#### H4 - Create the best documentation\n\nTEST\n\n##### H5 - Create the best documentation\n\nTEST\n\n###### H6 - Create the best documentation\n\n

我写了一个 expression 来查找最后一次出现的“#”。在我得到最后一个 # 之后,我想从那里取出整个字符串,直到只找到第一个 \n。

例如,当我在 ###### 中找到最后一个“#”时,H6 - 创建最好的文档\n\n。然后想提取#H6 - 创建最好的文档

有没有办法用正则表达式做到这一点?我在 JavaScript 代码中使用它,那么是否可以使用正则表达式执行此操作?

5 个答案:

答案 0 :(得分:0)

试试这个:

(#[^#]*)$

示例:

"##H2\n\ncontent.\n\n## Headers\n\nTEST\n\n# H1 - Create the best documentation\n\nTEST\n\n# H2 - Create the best documentation\n\nTEST\n\n### H3 - Create the best documentation\n\nTEST\n\n#### H4 - Create the best documentation\n\nTEST\n\n##### H5 - Create the best documentation\n\nTEST\n\n###### H6 - Create the best documentation\n\n".match(/(#[^#]*)$/)

// ouput: [ "# H6 - Create the best documentation\n\n", "# H6 - Create the best documentation\n\n" ]

修剪的例子

const trimmed = "##H2\n\ncontent.\n\n## Headers\n\nTEST\n\n# H1 - Create the best documentation\n\nTEST\n\n# H2 - Create the best documentation\n\nTEST\n\n### H3 - Create the best documentation\n\nTEST\n\n#### H4 - Create the best documentation\n\nTEST\n\n##### H5 - Create the best documentation\n\nTEST\n\n###### H6 - Create the best documentation\n\n".match(/(#[^#]*)$/).pop().replace(/\s*$/, "")

// "# H6 - Create the best documentation"

限制输出到第一个换行符示例:

const trimmed2 = "##H2\n\ncontent.\n\n## Headers\n\nTEST\n\n# H1 - Create the best documentation\n\nTEST\n\n# H2 - Create the best documentation\n\nTEST\n\n### H3 - Create the best documentation\n\nTEST\n\n#### H4 - Create the best documentation\n\nTEST\n\n##### H5 - Create the best documentation\n\nTEST\n\n###### H6 - Create the best documentation\n\nnext line"
.match(/(#[^#]*)$/)
.pop()
.replace(/(?:\n+.*)?$/, "")

// "# H6 - Create the best documentation"

答案 1 :(得分:0)

let myRegex = new RegExp(/^[^]*(#[^]*)$/);
let matches = myRegex.exec("%sdfsdf#\n##sdgsdf\ngdf#sadgofofo#jhjhj\nhjh");
let myResult = matches[1]

模式分解是这样的:

'^' start at the beginning of the string
'[^]*' match everything including line breaks as much as you can including any '#'
'(#[^]*)' group a match of the final '#' and anything following it, including line breaks
'$' to the end of the string

Regex 是贪婪的,所以它会使用上面的模式吸收所有内容并落在最后的“#”上。 '[^]' 用于匹配换行符。 我从这里的 MDN 站点示例中了解到: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

这是一个很棒的正则表达式测试站点,如果您还没有找到的话:

https://regex101.com/

这是上述解决方案的JS Fiddle https://jsfiddle.net/y8qbe705/

答案 2 :(得分:0)

(?:#[^#\\]+(?!.*#))

匹配来自 # 但不再是 #\ 并且应该是最后一个匹配 #。这不是最好的解决方案,但有效!输出。

# H6 - Create the best documentation

答案 3 :(得分:0)

我会在这里使用正则表达式替换:

var input = "##H2\n\ncontent.\n\n## Headers\n\nTEST\n\n# H1 - Create the best documentation\n\nTEST\n\n# H2 - Create the best documentation\n\nTEST\n\n### H3 - Create the best documentation\n\nTEST\n\n#### H4 - Create the best documentation\n\nTEST\n\n##### H5 - Create the best documentation\n\nTEST\n\n###### H6 - Create the best documentation\n\n";
var output = input.replace(/^.*(#.*?)(?=\n|$)/s, "$1");
console.log(output);

正则表达式替换删除所有内容,直到但不包括最后一个 # 符号。然后我们匹配并捕获所有进行中的内容,直到最近的换行符,但不包括最近的换行符。

这里是正则表达式模式的解释:

^             from the start of the string
    .*        consume all content until the LAST
    (#.*?)    # followed by all content up to the first (capture in $1)
    (?=\n|$)  newline character or the end of the input

然后,我们仅替换为 $1 中的捕获主题标签。

答案 4 :(得分:0)

匹配以 # 字符开头,以字符串结尾结尾,中间有非 # 字符 (regex101) 的字符串:

const input = "##H2\n\ncontent.\n\n## Headers\n\nTEST\n\n# H1 - Create the best documentation\n\nTEST\n\n# H2 - Create the best documentation\n\nTEST\n\n### H3 - Create the best documentation\n\nTEST\n\n#### H4 - Create the best documentation\n\nTEST\n\n##### H5 - Create the best documentation\n\nTEST\n\n###### H6 - Create the best documentation\n\n";

const output = input.match(/#[^#]+$/s);

console.log(output);