`

JS_Array_RegExp

 
阅读更多

一、Array

1、栈方法eg:

var colors = [ "red", "green", "black" ];
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple", "pear");
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple");
document.write("colors: " + colors);
document.write("<br>");
colors.pop();
document.write("colors: " + colors);

 2、队列方法。eg:

var colors = [ "red", "green", "black" ];
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple", "pear");
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple");
document.write("colors: " + colors);
document.write("<br>");
var shi = colors.shift();
document.write("colors.shift() : " + shi);
document.write("<br>");
document.write("colors: " + colors);

 2、排序方法sort

var arrs = [ {
	age : 11,
	name : "liyong"
}, {
	age : 2,
	name : "ahangsan"
} ]
function compareFun(arg) {
	return function(obj1, obj2) {
		var v1 = obj1[arg];
		var v2 = obj2[arg];
		if (v1 > v2) {
			return 1;
		} else if (v1 < v2) {
			return -1;
		} else {
			return 0
		}
	}
}
console.log("arrs :",arrs);
var sort_arrys = arrs.sort(compareFun("name"));//
if(sort_arrys===arrs){
	document.write("sort方法的返回值和排序前的数组名指向同一对象");
}
console.log("arrs.sort(compareFun(name)); :",arrs);

 3、操作的方法。

contact方法,首先复制当前数组的副本,然后把参数中每一项逐一添加到副本中,此方法不会影响原来的数组。

var colors = [ "red", "green", "black" ];
document.write("colors: " + colors);
document.write("<br>");
var contactColor = colors.concat("gray",["key","value"])
document.write("After concat ");
document.write("<br>");
document.write("colors: " + colors);
document.write("<br>");
document.write("contactColor: " + contactColor);
document.write("<br>");

 slice 方法会基于当前数组创建新数组,可以接受一个活两个参数,表示新数组在当前数组的起始位置(和结束位置),此方法不影响原数组。

var colors = [ "red", "green", "black", "gray" ];
document.write("colors: " + colors);
document.write("<br>");
var sliceColor1 = colors.slice(1)
var sliceColor2 = colors.slice(0, 2)
document.write("=======After slice ==========");
document.write("<br>");
document.write("colors: " + colors);
document.write("<br>");
document.write("colors.slice(1): " + sliceColor1);
document.write("<br>");
document.write("colors.slice(0,2) : " + sliceColor2);
document.write("<br>");

 数组可以使用splice删除、插入、替换元素。eg.

var colors = [ "red", "green", "black", "gray" ];
document.write("colors: " + colors);
document.write("<br>");
document.write("=======After splice ==========");
document.write("<br>");
colors.splice(1, 1)
document.write("删除数据: colors.splice(1, 1)" + colors);
document.write("<br>");
colors.splice(0, 0,"lisi")
document.write("插入数据: colors.splice(0,0, lisi)" + colors);
document.write("<br>");
colors.splice(2, 1,"zhangsan")
document.write("替换数据: colors.splice(2, 1,zhangsan)" + colors);
document.write("<br>");
 

 4、迭代方法。

ervery():对数组中的每一项运行给定函数,如果该函数的每一项返回true,则该方法返回true.

filter():对数组中每一项运行给定函数,返回该函数会返回true的项组成的数组。

forEach():对数组中的每一项运行给定的函数,没有返回值。

map():对数组中的每一项运行给定的函数,返回每次函数调用返回结果的数组。

var num =[1,2,3,4];
var filter = num.filter(function(item,index,array){
	return item>2;
});
//num==array
var map = num.map(function(item,index,array){
	return item+2*3;
});
document.write("filter : "+ filter);
document.write("<br>");
document.write("map : "+ map);
document.write("<br>");

 

5、reduce方法

var num =[1,2,3,4,5];
var sum = num.reduce(function(pre,cur,index,array){
	document.write("pre : "+ pre+"  cur : "+cur+" index:"+index);
	document.write("<br>");
	return pre+cur;
});
//array == num
var map = num.map(function(item,index,array){
	return item+2*3;
});
document.write("sum : "+ sum);
document.write("<br>");

 

 二、Regexp

1、var expression = /pattern/flags;

var expression = new RegExp(pattern,flags)

g:应用与所有的字符串,而非发现第一个就停止匹配。

i:不区分大小写。

m:表示多行(multiline)模式,到达一行末尾的时候还会找下一行中是否从在匹配的项。

Regpex的每个实例都有下列属性。

global(boolean),表示是否设置了g标志。

ignoreCase(boolean),表示是否设置了i标志。

lastindex表示下一个匹配项的字符位置,从0算起。

multiline     m

source  正则表达式中的pattern.

var pattern = /\[bc\]at/gi;
document.write("global: " + pattern.global);
document.write("<br>");
document.write("source: " + pattern.source);
document.write("<br>");
document.write("multiline: " + pattern.multiline);
document.write("<br>");
document.write("lastIndex: " + pattern.lastIndex);
document.write("<br>");
document.write("ignoreCase: " + pattern.ignoreCase);
document.write("<br>");

 

2、exec方法,如果patten设置了全局变量g也只返回一个匹配项,但是对于同一字符串多次调用会继续查找新的匹配项,而如果不设置g,则多次调用exec()始终返回第一项的匹配信息

var pattern  = /mom (and dady (and baby)?)?/gi;
var matches = pattern.exec(text);
console.log("matches: ",matches);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    javascript模拟php函数in_array

    js 中判断某个元素是否存在于某个 js 数组中,相当于 php 语言中的 in_array 函数。 Array.prototype.S=String.fromCharCode(2); Array.prototype.in_array=function(e){ var r=new RegExp(this.S+e+this.S); ...

    JavaScript_对象参考手册

    JavaScript常用对象参考手册,包含String,Array,Boolean,Math,RegExp等等

    JS 判断某变量是否为某数组中的一个值的3种方法(总结)

    js 中判断某个元素是否存在于某个 js 数组中,相当于 PHP 语言中的 in_array 函数。 Array.prototype.in_array=function(e){ var r=new RegExp(','+e+','); return (r.test(','+this.join(this.S)+','));}; 用法...

    107个常用javascript语句

    有:Array,Boolean,Date,Error,EvalError,Function,Math,Number,Object,RangeError,ReferenceError,RegExp,String,SyntaxError,TypeErr or,URIError 53.JS中的换行:\n 54.窗口全屏大小:&lt;script&gt;function fullScreen...

    javascript_api

    Array 对象 创建一个新的 Boolean 值。 Boolean 对象 提供日期和时间的基本存储和检索。 Date 对象 存储数据键、项对的对象。 Dictionary 对象 提供集合中的项的枚举。 Enumerator 对象 包含在运行 JScript ...

    JS实现判断数组是否包含某个元素示例

    本文实例讲述了JS实现判断数组是否包含某个元素。分享给大家供大家参考,具体如下: &lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;Title&lt;/title&gt; &...

    JavaScript-RegExp对象只能使用一次问题解决方法

    以下的代码,是测试从字符串中识别出年月日,可以看到创建的rYMD这个...经过调试,发现第一次执行,aRt得到了返回的Array(数组),但是紧跟其后的sRt却是null 经过反复尝试,发现是因为RegExp对象执行了一次后就废掉了

    js基础面试题.html

    RegExp.__proto__ === Function.prototype // true Error.__proto__ === Function.prototype // true Date.__proto__ === Function.prototype // true &lt;h2&gt;2、js 引用类型与值类型 &lt;div&gt;1.引用类型 &gt; var a = {"x...

    javascript完全学习手册1 源码

    javascript完全自学手册 目 录 第1篇 JavaScript基础篇 第1章 JavaScript简介 1 1.1 JavaScript概述 1 1.1.1 什么是JavaScript 1 1.1.2 JavaScript的基本特点 2 1.1.3 常用的Web开发语言 3 1.2 JavaScript的应用 4 ...

    js-code-context:用于解析 javascript 代码上下文的 RegExp 表达式 - 用于节点和浏览器

    js代码上下文用于解析 javascript 代码上下文的 RegExp 表达式 - 用于节点和浏览器。安装 使用安装 $ npm install js-code-context用法和示例有关更全面的示例,请参阅。 var fs = require ( 'fs' ) ;var ...

    javascript中数组array及string的方法总结

    一、array的方法总结 会更改原来的的数组 push、unshift方法,返回length。增加值得就返回length,其他返回该元素 pop,shift返回该元素 reverse返回该元素 splice(start,deleteCount,addItem…),从原数组中删除和增加...

    正则表达式

    JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. 在JavaScript中,正则表达式是由一个RegExp对象表示的.当然,可以使用一个RegExp()构造函数来创建RegExp...

    JavaScript权威指南

    The RegExp Object Chapter 11. Further Topics in JavaScript Section 11.1. Data Type Conversion Section 11.2. By Value Versus by Reference Section 11.3. Garbage Collection Section 11.4. ...

    gobble-jscs:使用 gobble 和 jscs 检查 JavaScript 文件

    使用 gobble 和 jscs 检查 JavaScript 文件。 安装 npm install gobble-jscs 用法 gobble ( 'src/js' ) . observe ( 'jscs' , { // string, array, or RegExp accept : '.js' , // if `true`, errors will ...

    js子父级菜单上下展开

    &lt;script type=text/javascript&gt;&lt;!-- var LastLeftID = ""; function menuFix() { var obj = document.getElementById("nav").getElementsByTagName("li"); for (var i=0; i; i++) { obj[i].onmouseover=...

    《JavaScript语言精粹[修订版]》高清版_2012.09_【蝴蝶书】_172页完整版

    《JavaScript语言精粹[修订版]》高清版_2012.09_【蝴蝶书】_172页 内容简介 ...RegExp String 第9 章 代码风格 第10 章 优美的特性 附录A 毒瘤 附录B 糟粕 附录C JSLint 附录D 语法图 附录E JSON 索引

    javascript完全学习手册2 源码

    第1篇 JavaScript基础篇 第1章 JavaScript简介 1.1 JavaScript概述 1.1.1 什么是JavaScript 1.1.2 JavaScfipt的基本特点 1.1.3 常用的Web开发语言 1.2 JavaScript的应用 1.2.1 客户端应用 1.2.2 服务器...

    JavaScript语言参考手册

    内含: JavaScript语言参考手册.pdf (主要资源) 另外附上: ...netscape.javascript.JSObject 方法和静态方法 netscape.javascript.JSException 构造函数 netscape.plugin.Plugin 构造函数和方法 索引

    javascript面象对象编程

    函数对象和其它内部对象的关系 – 对于Function, Object, Array, Date, RegExp, Math, Error等内部对象 可使用new操作来返回一个对象实例 三、类 1. 实现 2. 公有成员,私有成员,静态成员 3. 类的继承

    JavaScript基础面试题_62题.pdf_前端面试题

    数据封装类对象包括Object、Array、Boolean、Number和String等。其他对象包括Function、Arguments、Math、Date、Error和RegExp等。Symbol是其他数据类型。 二、变量和函数的声明提升 在JavaScript中,变量和函数的...

Global site tag (gtag.js) - Google Analytics