[英]Why doesn't this regex match in ruby but matches in any other language?
以下內容可在Rubular.com
上Rubular.com
但在ruby中似乎不匹配:
字符串:
str = "<em>really</em>inexpensive"
目標:在任何結束標記之后添加一個空格,之后沒有任何空格。
正則表達式 :
str.gsub("/(<\/[a-zA-Z]+>)(\S)/","\1 \2")
它應該返回"<em>really</em> inexpensive"
您應該使用正則表達式文字( /.../
),而不是字符串( "..."
)。 並在替換字符串中轉義\\
。 (在下面的示例中,我使用了單引號形式的字符串)
str = "<em>really</em>inexpensive"
str.gsub(/(<\/[a-zA-Z]+>)(\S)/, '\1 \2') # '\1 \2' == "\\1 \\2"
# => "<em>really</em> inexpensive"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.