簡體   English   中英

正則表達式在字符串中最后一次出現“#”后獲取整個字符串

[英]Regex to get the entire string after last occurrence of “#” in a string

您好,我是使用正則表達式的新手。 我有以下字符串-

##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

我寫了一個表達式來查找最后一次出現的“#”。 在我得到最后一個 # 之后,我想從那里取出整個字符串,直到只找到第一個 \n。

例如,當我在 ##### # H6 中找到最后一個“#”時 - 創建最佳文檔\n\n。 然后想提取#H6——創建最好的文檔

有沒有辦法用正則表達式做到這一點? 我在 JavaScript 代碼中使用它,那么可以用正則表達式來做到這一點嗎?

嘗試這個:

(#[^#]*)$

例子:

"##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"

將 output 限制到第一個換行符示例:

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"
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

正則表達式是貪婪的,所以它會吸收所有內容並使用上面的模式落在最后的“#”上。 '[^]' 用於匹配換行符。 我從這里的 MDN 站點示例中了解到: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

如果您還沒有找到它,這里是一個很棒的正則表達式測試站點:

https://regex101.com/

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

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

#開始匹配,但不再匹配#\ ,它應該是最后一個匹配# 這不是最好的解決方案,但有效.. 輸出。

# H6 - Create the best documentation

我會在這里使用正則表達式替換:

 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中的捕獲主題標簽。

匹配以#字符開頭並在字符串末尾終止且中間有非#字符的字符串 ( 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM