jquery - Javascript replace with regex not affecting string -
i have webapp task manager.
the app recognises day/month in string.
i have function replicate selected tasks today, trying make function update date in string.
so, example, do task! 29/5
become do task! 1/6
.
the function looks this:
var d = new date(); var mon = d.getmonth()+1; var day = d.getdate(); $('input.replicatecheck:checkbox:checked').each(function(){ //string of row (nam) var nam = $(this).parent().find('input.row-name').val(); //replace existing date current date nam = nam.replace('\d{1,2}\/\d{1,2}',day+'/'+mon); console.log(nam); });
however isn't replacing date in string.
the issue line:
nam = nam.replace('\d{1,2}\/\d{1,2}',day+'/'+mon);
why isn't working?
edit following answers, requested, here working version of i'm trying achieve:
$('button#go').click(function() { var text = $('#testinput').val(); var d = new date(); var mon = d.getmonth() + 1; var day = d.getdate(); newtext = text.replace(/\d{1,2}\/\d{1,2}/, day + '/' + mon); alert(newtext); });
* { width: 100%; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; padding: 10px; } button { margin-top: 20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="testinput" value="the quick brown fox jumps on 26/5" /> <br/> <button id="go">go!</button>
you're missing regex delimiters:
nam = nam.replace(/\d{1,2}\/\d{1,2}/, day+'/'+mon);
Comments
Post a Comment