Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

regex - Remove using regular expression in javascript

I have a javascript string contains html table like this:

  <div class="nobrtable"><table class="at"><TBODY><TR><td> FY

  </TD><td> Profit Margin

  </TD><td> Asset Turnover

   </TD><td> RoA

   </TD><td> Leverage Ratio

   </TD><td> RoE

   </TD><td> Cash Conversion

   </TD><td> Cash RoE

   </TD><td> RoC

    </TD></TR><TR><td> 2002

     </TD><td> 5.1%

    </TD><td> 1.42

     </TD><td> 7.2%

      </TD><td> 127%

      </TD><td> 9.2%

      </TD><td> 163%

      </TD><td> 14.9%

      </TD><td> 16.9%

      </TD></TR>

      </TD></TR></TBODY></table></div> 

How could I using regular expression to remove all the ' ' in the table? Otherwise it is too long.

I tried using

ele = ele.replace(/
/g, ' ');

It will replace all the ' ' in my string. I just want to remove all the ' ' in all the html tables.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

ele = ele.replace(/s+</TD>/g,' </TD>')

/m on the regex means match multi-line which helps matching on multiple 's in a string.

UPDATE

  • dropped the /m modifier
  • changed s* to s+
  • introduced a whitespace character in the replacement string as browsers will render a whitespace character after your text in the TD, which you may want (though better to use no space and use CSS for padding/margins

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...