html span wrap,html - wrap word in span vanilla Javascript - Stack Overflow

I have a funtion that return to me a list of word (with the previous and next qord). I am able to locate the word in the text correctly using a RegEx and knowing the previous and next word (so I find only one time the word and not all of the same). I am actually using the replace function of Javascript to wrap it inside a SPAN :

var re = RegExp("(?:^\\W*|(" +

motBefore.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + ")\\W+)" +

motErreur.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "(?:\\W+(" +

motAfter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + ")|\\W*$)", "g");

document.getElementById('edth_corps').innerHTML =

document.getElementById('edth_corps').innerText

.replace(re, motBefore + " " + '

'οncοntextmenu="rightClickMustWork(event, this);return false"> '

+ motErreur + '

' + " " + motAfter);

But the fact is that because I am placed on the group of these three word, I have to give him back the word before and after.

The issue I got with this tech is that I loose all punctuation and the more important , if there is a font tag (or bold, italic, underline) I loose them.

I'd like to wrap around this word a span like it is the child of the current node where this word is without touching the previous and next word (they are suposed to be here so we have the right word and not another occurence of this word).

But I have no idea how to do it since I can only use old Javascript (IE compiler is set to compatibily mode for IE5, and this function is the only one I can't manage to rewrite for this version). I can't use library such a Jquery (I have seen a lot of solution with this plug in).

Could someone point me in the right direction ? Could an insertBefore work even if the Error Word has no parent around him but is only part of a sentence ?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值