《JavaScript经典实例》各节中的完整代码解决了常见的编程问题,并且给出了在任何浏览器中构建Web应用程序的技术。只需要将这些代码示例复制并粘贴到你自己的项目中就行了,可以快速完成工作,并且在此过程中学习JavaScript的很多知识。
5种基本类型:字符串、数值、布尔值、null、undefined,有3个有对应的构造方法对象:string、Number、Boolean
This is a list of items: cherries, limes, oranges, apples.
['cherries','limes','oranges','apples']
if(typeof unknowVariable === 'string' && unknowVariable.length > 0) {...} | 字符 | 匹配 | 例子 |
|---|---|---|
| ^ | 匹配输入的开头 | /^This/ 匹配This is... |
| $ | 匹配输入的结束 | /end$/ 匹配This is the end |
| * | 匹配0次或多次 | /se*/ 匹配s seeee或se |
| ? | 匹配0次或1次 | /ap?/ 匹配apple and and |
| + | 匹配1次或多次 | /ap+/ 匹配apple 但是不匹配and |
| {n} | 严格匹配n次 | /ap{2}/ 严格匹配apple 但是不匹配apie |
| {n,} | 匹配n次或多次 | /ap{2,}/ 匹配apple中的p,但是不匹配apie中的p |
| {n,m} | 至少匹配n次,之多匹配m 除换行以外的任何字符 | /ap{2,4}/ 匹配apppppple中的4个p /a.e/ 匹配ape和axe |
| [...] | 方括号中的任何字符 | /a[px]e/ 匹配ape axe 但是不匹配apxe |
| [^...] | 除了方括号以外的任何字符 | /a[^px]/ 匹配ale 但是不匹配ape axe |
| b | 匹配单词边界 | /bno/ 匹配nono中的第一个no |
| B | 匹配非单词边界 | /Bno/ 匹配nono中的第二个no |
| d | 数字0到9 | /d{3}/ 匹配Now in 123 中的123 |
| D | 匹配任何非数字字符 | /D{2,4}/ 匹配Now in 123 中的Now in |
| w | 匹配任何单词字符(字母、数组和下划线 | /w/ 匹配javaScript中的j |
| W | 匹配任何非单词字符(非字母、数组和下划线) | /W/ 匹配100%中的% |
| n | 匹配一个换行 | |
| s | 一个单个的空白字符 | |
| S | 一个单个的非空白字符 | |
| t | 一个制表符 | |
| (x) | 捕获括号 | 记住匹配的字符 |
var searchString2 = "Now is the time and this is the time and that is the time";
var parttern = /tw*e/g; //w 匹配任何单词字符
var matchArray;
var str = "";
//用regexp exec检查模式,如果不为空,处理它
while((matchArray = parttern.exec(searchString2)) != null) {
str += "at " + matchArray.index + " we found " + matchArray[0] + "n";
}
console.log(str);
// at 7 we found the
// at 11 we found time
// at 28 we found the
// at 32 we found time
// at 49 we found the
// at 53 we found time
找到并突出显示一个模式的所有实例
";
//插入页面
document.getElementById("searchResult").innerHTML = resultString;
}
| 字符 | 替换文本 |
|---|---|
| $1、$2、...、$99 | 与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。 |
| $& | 与 regexp 相匹配的子串。 |
| $` | 位于匹配子串左侧的文本。 |
| $' | 位于匹配子串右侧的文本。 |
| $$ | 允许替换中有一个字面值美元符号($) |
| $n | 插入使用RegExp的第n次捕获圆括号的值 |
var intervalId = null;
document.getElementById("redbox").addEventListener('click', startBox, false);
function startBox() {
if(intervalId == null) {
var x = 100;
intervalId = setInterval(function() {
x += 5;
var left = x + "px";
document.getElementById("redbox").style.left = left;
}, 500);
} else {
clearInterval(intervalId);
intervalId = null;
}
} var firstDate = new Date();
setTimeout(function() {
doEvent(firstDate);
}, 25000);
function doEvent() {
var secondDate = new Date();
var diff = secondDate - firstDate;
console.log(diff); //25001
} var sum = 0;
//使用querySelectorAll找到第二列的所有单元格
var cells = document.querySelectorAll("td:nth-of-type(2)");
for(var i=0, l=cells.length; i var radians = degrees * (Math.PI / 180);
var degrees = radians * (180 / Math.PI); var circleRadius = Math.min(elemengWidth, elemengHeight) / 2; var x = elemengWidth / 2;
var y = elemengHeight / 2;
将一个SVG圆放入到一个div元素中
Math.PI把角度转换为弧度,并在公式中使用该结果来求得圆弧的长度 var radians = degrees * (Math.PI / 180);
var arclength = radians * radians;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2826/viewspace-2799501/ ,如需转载,请注明出处,否则将追究法律责任。