简体   繁体   English

转换Ruby String gsub! javascript字符串替换方法

[英]Convert Ruby String gsub! to javascript string replace method

I have snip code in Ruby that used gsub! 我在使用gsub的Ruby中有片段代码! method as below 方法如下

# handle headings (H1-H6)
text.gsub!(/(<\/h[1-6]>)/i, "\n\\1") # move closing tags to new lines
text.gsub!(/[\s]*<h([1-6]+)[^>]*>[\s]*(.*)[\s]*<\/h[1-6]+>/i) do |s|
  hlevel = $1.to_i

  htext = $2
  htext.gsub!(/<br[\s]*\/?>/i, "\n") # handle <br>s
  htext.gsub!(/<\/?[^>]*>/i, '') # strip tags             
  hlength = 3

  case hlevel
    when 1   # H1, equal below
      htext = "\n" + htext.upcase + "\n" + ('=' * hlength)
    when 2   # H1, dashes below
      htext = "\n" + htext.upcase + "\n" + ('-' * hlength)
    else     # H3-H6, dashes below
      htext = "\n" + htext + "\n" + ('+' * hlength)
  end
  htext
end

Above snip will convert html string to plain text. 上面的代码片段会将html字符串转换为纯文本。 this process for H(s) tag. H(s)标签的此过程。

I need to port the snip to javascript that will use replace method Someone can help me, thanks 我需要将代码片段移植到将使用替换方法的javascript中,有人可以帮助我,谢谢

You might want to try this gist (JS implementation of gsub): 您可能需要尝试以下要点(gsub的JS实现):

https://gist.github.com/varunkumar/8572487 https://gist.github.com/varunkumar/8572487

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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