Chapter 4
Use the
String.split
method, passing in a comma (,) as delimiter.Word boundaries are particularly useful if you want a separate word, but being given a string that could match within another word:
var regexp = /\bfun\b/; var str = "The fun of functions is that they are functional."; var result = str.replace(regexp,"power");
There is no
Date
function that manipulates weeks, but we know that a week is 7 days at 24 hours a day, for a total of 168 hours. Use thegetHours
method to get the current dateâÂÂs hours, add this value, reset the hours, and then print out the date. Other approaches can also be used and are left for your own exploration:var dtNow = new Date(âÂÂâÂÂ); var hours = dtNow.getHours(âÂÂâÂÂ); hours+=168; dtNow.setHours(hours); document.writeln(dtNow.toString(âÂÂâÂÂ));
Math.floor
can be used to round the number down;Math.ceil
can round the number up.The answer is:
var str = "apple.orange-strawberry,lemon-.lime"; var regexp = /[\.|-]/g; var result = str.replace(regexp,','); var arrayValues = result.split(','); for (var i = 0; i < arrayValues.length; i++) { document.writeln(arrayValues[i] + "<br />"); }
Get Learning JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.