0

I have snip code in Ruby that used gsub! 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. this process for H(s) tag.

I need to port the snip to javascript that will use replace method Someone can help me, thanks

4
  • 2
    What do you have tryed so far? Commented Jan 12, 2015 at 9:43
  • @SG86, I want to convert html string to plain text uses javascript keep number order in ol tags Commented Jan 12, 2015 at 9:48
  • I think i know what you search for, but would be good to see the point where you need help. Where is your javascript? I mean stackoverflow is to help/support, not for completing issues? Commented Jan 12, 2015 at 13:55
  • it is the loop in gsub!. I can not find the same in replace Commented Jan 12, 2015 at 15:09

1 Answer 1

0

You might want to try this gist (JS implementation of gsub):

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.