简体   繁体   English

Javascript 剥离 substring 并附加它

[英]Javascript stripping out substring and appending it

I am trying to do something which should be simple but I'm having no luck.我正在尝试做一些应该很简单但我没有运气的事情。 I have a string like the following:我有一个如下字符串:

<hgc attr="something">late at</hgc>

and I need to strip out the last at prior to the closing html tag and append it, surrounded by its own tag to produce:我需要at关闭 html 标签和 append 之前去掉最后一个,由它自己的标签包围以产生:

<hgc attr="something">late </hgc><hgb>at</hgb>

Don't worry about the tags, they are custom.不用担心标签,它们是自定义的。

Update更新

That at word may be anything, even hgc and I will already have the word ahead of time to produce the appended html. at字可能是任何东西,即使是hgc和我也已经提前得到了这个字来生成附加的 html。

I have attempted to perform the following:我试图执行以下操作:

let markup = '<hgb>' + word + '</hgb>';

let applied = $element.html().slice(0, pos) + 
                  $element.html().slice(pos).replace(word, "") +
                  markup;

Try this one.试试这个。

 let str = '<hgc attr="something">late at</hgc>'; let reg = /\w+(?=<)/g; let word = str.match(reg)[0]; str = `${str.split(reg).join('')}<hgb>${word}</hgb>`; console.log(str);

Something like this should work.像这样的东西应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM