如何替换变量设置的 Replace() - Javascript



>我需要根据用户在表单上按下的单选按钮,将下面div 中的类 "sash" 更改为一个新类。div 存储在字符串中。

jData = '<div class="hotspot F1hs sash" id="F1” >’
var radios = $(".rectangleRadios.active").children("input").val(); 
var windowName = $.urlParam('windowid’); // This is the F1. It comes from a URL parameter and will change from page to page.
var updateFrame = new RegExp(windowName+'hs','g’);  //Creates the F1hs
var id = new RegExp('\" id=\"'+windowName,'g’); // Creates the “  id=F1

所以我正在尝试删除 F1hs 和 id 之间的任何类,然后将其替换为选定的"无线电"按钮。

jData = jData.replace(/updateFrame.+?id/, updateFrame+" "+radios+ id);

如何删除两个变量之间的内容并添加新类?

像这样的东西?

var jData = '<div class="hotspot F0hs sash" id="F1” ><div class="hotspot F1hs sash" id="F1” ><div class="hotspot F3hs sash" id="F1” >';
jData = jData.replace(/(<div.*?class=".*?F1hss+.*?).*?(".*)/gi, "$1NEWCLASS$2");

    console.log(jData);

输出:

<div class="hotspot F0hs sash" id="F1” ><div class="hotspot F1hs NEWCLASS" id="F1” ><div class="hotspot F3hs sash" id="F1” >

最新更新