[英]Replace specific new lines starting with “ #” in GO
我是GO的新手,我有點困惑。 我不知道我在做什么錯。
我想將markdown轉換為html,所以我需要找到每行以空格和#開頭的行,並替換為h1標簽。
如果我測試多行,則無法正常工作,而僅測試一行時,則可以正常工作。
例:
//this works
testtext := "#Some text"
expectedResult := "<h1>Some text</h1>"
//this fails
testtext :=`#test
##test2
####test4
###test3`
expectedResult := `<h1>test</h1>
##test
####test
###test`
//test
func TestReplaceHedding1(t *testing.T) {
text := `#test
##test2
####test4
###test3`
replaceHedding1(&text)
if text != "<h1>test</h1>##test\n####test\n###test" {
t.Errorf("expected <h1>test</h1>, got", text)
}
}
func replaceHedding1(Text *string) {
reg := regexp.MustCompile(`^\s*#{1}(.*?)$`)
*Text = reg.ReplaceAllString(*Text, "<h1>$1</h1>")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.