Reputation: 9001
I have a webapp task manager.
The app recognises day/month in a string.
I have a function that will replicate selected tasks for today, but I am trying to make the function update the date in the string.
So, for example, Do this task! 29/5
would become Do this task! 1/6
.
The function currently looks like 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 with current date
nam = nam.replace('\d{1,2}\/\d{1,2}',day+'/'+mon);
console.log(nam);
});
however it isn't replacing the date in the string.
The issue will be this line:
nam = nam.replace('\d{1,2}\/\d{1,2}',day+'/'+mon);
Why isn't this working?
Edit Following the answers, as requested, here is a working version of what I'm trying to 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>
Upvotes: 1
Views: 69
Reputation: 133403
You need to provide RegEx delimiter
nam = nam.replace(/\d{1,2}\/\d{1,2}/,day+'/'+mon);
^ ^
Upvotes: 3
Reputation: 785196
You're missing regex delimiters:
nam = nam.replace(/\d{1,2}\/\d{1,2}/, day+'/'+mon);
Upvotes: 3