Replace text in gsub brackets

I want to replace the text inside the brackets and has colonand u.

For example, it Here is a link [u:person]!will become Here is a link <a href="/user/person">Person</a>!I am not very good at regular expression, and I am having problems with \1and$1

Here is the regex that I'm using now:

string.gsub(/\[(\w*).*?\]/, "<a href='/user/\1'>\1</a>")
+4
source share
4 answers

You can use regex as follows:

/\[u\:([\S]+)\]/

and replace it with:

<a href='/user/#{$1}'>#{$1}</a>

Here's a breakdown of what this regular expression does:

  • First, we have \[, which is a literal symbol[
  • Further, we have uand \:, which are literal symbols uand, :accordingly,
  • ([\S]). , #{$1} . [\S]+ .
  • , \], ].

:

string.gsub('/\[u\:([\S]+)\]/', '<a href='/user/#{$1}'>#{$1}</a>')

: https://regex101.com/r/vK0iO2

0

, person :

/\[\w*:(.*?)\]/

:

"<a href=\"/user/#{$1}\">#{$1.capitalize}</a>"

$1, Ruby ( String- ):

string.gsub(/\[\w*:(.*?)\]/) { "<a href=\"/user/#{$1}\">#{$1.capitalize}</a>" }
+1

/\[\w*:(.*?)\]/, person u. , \1 \x01.

str = "Here is a link [u:person]!"
puts str.gsub(/\[\w*:(.*?)\]/, '<a href="/user/\1">\1</a>')
# => Here is a link <a href="/user/person">person</a>!
+1
\[[^\]]*u:([^\]]*)\]

. <a href='/user/\1'>\1</a>. .

https://regex101.com/r/gX5qF3/13

0
source

Source: https://habr.com/ru/post/1569384/


All Articles