猿问

JS replace 正则:求大神进来看一看,弄了一下午了没弄出来

原始字符串 var sourceText="";

sourceText有很多种情况。 比如:

情况1: sourceText="Where there is great love, <span style="color: #0000ff; font-weight: bold;">there</span> are always miracles.";

情况2:sourceText="Where there is great love, <span style="color: #0000ff; font-weight: bold;">there</span> <span style="color: #0000ff; font-weight: bold;">

are</span> always miracles.";

情况3:...

需要处理的字符串:there are always

现在的需求是: 从原始字符串中找到需要处理的字符串,然后高亮加黄色背景显示。但是原始字符串里可能所有的单词都已经被加粗和加字体颜色,这样直接找"there are always"这个字符串是找不到的。需要处理的字符串可能是一个单词,也可能是一个句子。

 

以sourceText为情况2和需要处理的字符串是there are always举例, 需要得到的结果是:Where there is great love, <span style="background-color:yellow;font-we;ight:inherit;"><span style="color: #0000ff; font-weight: bold;">there</span> <span style="color: #0000ff; font-weight: bold;">

are</span> always</span> miracles.

 

跪求大神不吝赐教!!!

能帮忙写出code最佳!!!

一下午没弄出来,实在是没招了!!!

在线等,

Help me...

红颜莎娜
浏览 881回答 35
35回答

狐的传说

你要先用正则把<>都去掉.然后就能获取到

红糖糍粑

用正则把<>都去掉? 然后怎么做呢? 吴老师如果能在百忙之中帮忙写一下code, 晚辈会万分感谢!

千巷猫影

@GEEKc:  [<].*?[>]  replace("")

翻阅古今

@吴瑞祥: 还是不太懂, 我是新人, 自己研究了一下午了。 吴老师能劳驾您帮忙写一段完整的code吗?  多谢。

宝慕林4294392

@GEEKc: 不能..什么语言什么环境.都不知道. 反正就是就用正则将"[<].*?[>]"替换为空

智慧大石

@吴瑞祥: 就是传统的js+html,  环境什么都行, Notepad++即可 多谢吴老师了。

跃然一笑

var sourceText ='<span style="color: #0000ff; font-weight: bold;">there</span>'; sourceText =sourceText .replace(/<span[^>]*>/g,"").replace(/<\/span>/g,""); console.log(sourceText );

繁花不似锦

 我并不是要取出there, 以sourceText为情况2和需要处理的字符串是there are always举例, 需要得到的结果是:Where there is great love, <span style="background-color:yellow;font-we;ight:inherit;"><span style="color: #0000ff; font-weight: bold;">there</span> <span style="color: #0000ff; font-weight: bold;"> are</span> always</span> miracles.

隔江千里

@司幸: 因为sourceText在这之前可能就已经有span标签存在字符串里面了,如果把这些处理掉的话,是能匹配到there are always,也能追加进去,但是之前的标签就没了。这里需要保存原来的标签。

SMILET

@GEEKc: 了解了,你的意思是 <span><span><span>there</span></span> are</span>aways 这种情况也是会出现的吗。替换后<span><span><span><span>there</span></span> are</span>aways </span>?
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答