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 O’Reilly online learning.
O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.