javascript regex replace the space between two groups -
i programmatically modifying large text file containing html/js code , need "mark" locations there space between 2 selectors, such between id , classname, in $('#btnone .subelement'). find these, need regex. however, falling down on replace bit.
regex101 demo -- correctly identifies 2 examples need replace inside space marker (such $#$#$ or something). (what wrong regex101 demo entire substring matched, not space in middle - that's need replace)
here code snippet replace ready go, same problem above.
$('button').click(function(){ var tmp = $('#orig').text(); tmp = tmp.replace("<br>", "\n"); tmp = tmp.replace(/\$\(['"].+ .*\)\./gi,'<span>how_target_only_the_space_char?</span>.'); $('#output').html(tmp); });
#output{margin-top:10px;background:palegoldenrod;} span{color:brown;font-weight:bold;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="orig"> function initsxschartbtns(){<br> var onebtnisalreadyclicked = false; $('#databuttons button').each(function(){ if ( $(this).hasclass('btn_depressed') ) onebtnisalreadyclicked = true; }); if (!onebtnisalreadyclicked){ $('#txtadvisory').val('approximately 25% registered.'); $('#btnone .subelement').addclass('btn_depressed'); $('#btntwo.sameelement').addclass('btn_depressed'); } } </div> <button>regex replace</button> <div id="output"></div>
you this:
tmp = tmp.replace(/(\$\(['"].+) (.*\)\.)/gm, '$1abc$2');
here $1
, $2
used refer capture groups in regexp. i've tried changed regexp little possible, i've added parentheses create capture groups.
replace
allows pass function second argument, allowing control replacement more precisely.
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/replace
Comments
Post a Comment