简体   繁体   中英

Why doesn't this regex match in ruby but matches in any other language?

The following works on Rubular.com but doesn't seem to match in ruby:

The string:

str = "<em>really</em>inexpensive"

Objective: Add a space after any closing tag without any space after it.

The regex :

str.gsub("/(<\/[a-zA-Z]+>)(\S)/","\1 \2")

It should give back "<em>really</em> inexpensive"

You should use regular expression literal ( /.../ ), not string ( "..." ). And escape the \\ in the replacement string. (I used single-quote version of string in the following example)

str = "<em>really</em>inexpensive"
str.gsub(/(<\/[a-zA-Z]+>)(\S)/, '\1 \2') # '\1 \2' == "\\1 \\2"
# => "<em>really</em> inexpensive"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM