JavaScript正则表达式替换忽略HTML标签的字符 [英] JavaScript regex to replace a character ignoring HTML tags

查看:149
本文介绍了JavaScript正则表达式替换忽略HTML标签的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再一次,我需要一些帮助,我感谢你们在这里愿意帮助。

我想实现一个JavaScript函数的正则表达式,它将替换包含在一个HTML标记,但完全忽略了标记。

例如:

我需要查找并替换的字符串是美元符号($),
和HTML我正在处理的内容如下所示:

 < div class =myClass> 
< input type =radiovalue =$ 233.93name =myInputid =myInputId>
< label>
$ 36。< sup> 07< / sup>
< / label>
< / div>

正如您所看到的,我在输入标记的value属性中有一个美元金额,而另一个
所有这些HTML都包含在一个td(表格单元格)中,而td(表格单元格)又是一个表格的一部分,为此我实现了按列排序功能。 >
我的问题是我还没有能够想出一个完全忽略HTML标签的正则表达式,并且只匹配标签标签内的字符串( $ 36< sup> 07<< ; / sup> )。

我把这个正则表达式放在一起: / [$](?=(\d {2,5} [。 <]))/

这个正则表达式可以工作,但只有一半,因为它匹配值属性值的第一个美元符号,授予正则表达式它没有设置到全球,因为它会取代所有的美元符号,我不想这样做,我也无法改变HTML的结构。



任何帮助将不胜感激。
谢谢

解决方案

您可以使用jQuery轻松解决此问题:

  jQuery.trim(jQuery('label')。text())

这会为你去掉标签,并生成 $ 36.07 ,然后你可以用更简单的正则表达式进行测试。





(如果你目前没有使用jQuery,并且不想使用它,你仍然可以看看它的源代码,已经实现了 .text()函数来模拟它。)




$ b $嗯,重新读你的问题,你可能会问别的 - 检索包含 $ (并忽略输入)的所有标签,你可以这样做:

  jQuery('label:contains($)')



  jQuery('label')。each(checkForDollars); 

函数checkForDollars()
{
if(jQuery(this).text()。matches(/ \ $ \d {2,5} /)})
{
//做某事
}


Once again, I need some help and I appreciate you all for being here willing to help.
I'm trying to implement a regex for a JavaScript function that will replace a string of text contained in an HTML tag, but ignoring the tag altogether.
For example:
The string I need to find and replace is the dollar sign ($), and the HTML that I'm working with looks like this:

<div class="myClass">
    <input type="radio" value="$233.93" name="myInput" id="myInputId">
    <label>
        $36.<sup>07</sup>
    </label>
</div>

As you can see, I have a dollar amount in the value attribute of the input tag, and another dollar amount inside the label tag.
All this HTML is contained within a td (table cell), which in turn is part of a table, for which I'm implementing a sorting by column functionality.
My problem is that I have not been able to come up with a regex that would ignore the HTML tags altogether and match only the string inside my label tag ($36.<sup>07</sup>).
I put together this regex: /[$](?=(\d{2,5}[.<]))/
This regex works, but only half way, because it matches the first dollar sign in the value of the value attribute, granted the regex it's not set to global, because it would replace all dollar sign and I don't wan't to do that, and I cannot change the structure of the HTML either.

Any help would be greatly appreciated. Thanks

解决方案

You can solve this easily with jQuery:

jQuery.trim( jQuery('label').text() )

That will strip the tags for you, and produce $36.07 which you can then test with a much simpler regex.


(If you're not currently using jQuery, and don't want to use it, you can still take a look at the source code for it and see how they've implement the .text() function in order to emulate it.)


Hmmm, Re-reading your question, you might be asking something else - to retrieve all labels containing $ (and ignore the inputs) you can do:

jQuery('label:contains($)')

or

jQuery('label').each( checkForDollars );

function checkForDollars()
{
    if ( jQuery(this).text().matches(/\$\d{2,5}/) } )
    {
        // do something
    }
)

这篇关于JavaScript正则表达式替换忽略HTML标签的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆