Replace string Between 2 tags in Jquery

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Replace string Between 2 tags</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>

<script>
var a="<NewDataSet xmlns=\"\"> <Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"> <start> <Sm_Scheme_Id>10</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.5000</Sm_Buy_Brok> <Sm_Sell_Brok>0.5000</Sm_Sell_Brok> </start> </Table> <Table diffgr:id=\"Table2\" msdata:rowOrder=\"1\"> <start> <Sm_Scheme_Id>1</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.0050</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start> </Table> <Table diffgr:id=\"Table3\" msdata:rowOrder=\"2\"> <start> <Sm_Scheme_Id>11</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.3500</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start> </Table> <Table diffgr:id=\"Table4\" msdata:rowOrder=\"3\"> <start> <Sm_Scheme_Id>12</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>1.0000</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start> </Table> <Table diffgr:id=\"Table12\" msdata:rowOrder=\"11\"> <start> <Sm_Scheme_Id>21</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.2500</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start> </Table> <Table diffgr:id=\"Table13\" msdata:rowOrder=\"12\"> <start> <Sm_Scheme_Id>31</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.3000</Sm_Buy_Brok> <Sm_Sell_Brok>0.3000</Sm_Sell_Brok> </start> </Table> <Table diffgr:id=\"Table14\" msdata:rowOrder=\"13\"> <start> <Sm_Scheme_Id>41</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>2.0000</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start> </Table> <Table diffgr:id=\"Table15\" msdata:rowOrder=\"14\"> <start> <Sm_Scheme_Id>51</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.1250</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start> </Table> </NewDataSet>     ";
alert(a);
var newString="<start>";
var b=a.replace(/<\/Table>.+?<start>/g,newString);
prompt("b  ",b);   // replacing all occurence of <Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"> with <start>

var c=b.replace(/<NewDataSet xmlns=\"\">.+?<start>/g,newString);
prompt("c  ",c);   // replacing 1st occurence of <Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"> with <start> of variable b

var d=c.replace(/<\/Table>.+?<\/NewDataSet>/g,'');  // replacing </Table></NewDataSet> of variable c(last 2 tags) to blank value   
prompt("d  ",d);

</script>
</body>
</html>


Note: Make sure that Jquery file is the first script loaded on your page.

No comments:

Post a Comment