There are for example divas:
<div name="klass[1][2]"></div> How can I check the value inside the second square brackets (in this case "2") and replace it with the value I need?
How can I check the value inside the second...">
There are for example divas:
<div name="klass[1][2]"></div> How can I check the value inside the second square brackets (in this case "2") and replace it with the value I need?
The replace method allows you to use your function to calculate the replacement.
function change2ndInd(str, i1, i2, offset) { if(i2==2) return "["+i1+"]["+i2*3+"]"; else return str; } var result = "klass[1][2]".replace(/\[(\d+)\]\[(\d+)\]/gi, change2ndInd); alert(result); // klass[1][6] Many ways, like so
$(function() { value = 'замена'; var data = $('div').attr('name').replace(/(.+)(\[.+?\])\[(.+?)\]/gi, '$1$2[' + value + ']'); $('div').attr('name', data); $('div').after(data); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div name="klass[1][2]"></div> Source: https://ru.stackoverflow.com/questions/521261/
All Articles