js正則和數組


 

什么是 RegExp?

RegExp 是正則表達式的縮寫。

RegExp 對象有 3 個方法:test()、exec() 以及 compile()

test() 方法檢索字符串中的指定值。返回值是 true 或 false。

exec() 方法檢索字符串中的指定值。返回值是被找到的值。如果沒有發現匹配,則返回 null。

compile()

compile() 方法用於改變 RegExp。

compile() 既可以改變檢索模式,也可以添加或刪除第二個參數。

 

正則的對象分為那幾種?

 

 

 

 

 

 

 正則例子:

 

var patt = new RegExp("javascript");
var res = patt.test('this is javascript course');                                      //檢查有沒有js
console.log(res);                                                        //有
patt = /javascript/;
patt = /JavaScript/i;                                                  //對大小寫不敏感
res = patt.test("this is javascript show time");
console.log(res);

test方法
//[] 方括號
res = /[abc]/.test('la');                                                       //錯
// res = /[^abc]/.test('lue');                                                      //對
// res = /[0-9]/.test('this is a test');                                                 //錯
// res = /[a-z]/.test('234235453245');                                                //錯
// res = /php|javascript|ios/i.test('PHP');                                               //或邏輯 對
console.log(res);

 

//元字符
res = /./.test('\n');                                                      ///n/r省略換行符和行結束符 其余為真
res = /./.test('this is a test');                                                      //真
res = /\w/.test('hello nana ')                                                 // 什么不是單詞字符[a-zA-Z0-9]
res = /\w/.test('!#@w');
res = /\W/.test('!#%9');                                                       //有9執行小寫的
console.log(res);                                                        //[^a-zA-Z0-9] 取相反
res = /\s/.test('hello world');                                                    //空白字符
res = /\S/.test('');                                                          //非空白
// res = /\bgo/.test('good');                                                      //開頭和結尾邊界
// res = /o\b/.test('good');
// res = /d\b/.test('good');
// res = /o\B/.test('good');                                                    //g也是正確的 //非單詞邊界
console.log(res);
//量詞
res = /o+/.test('google');                                                       //>=1
res = /o*/.test('google');                                                        //>=0
res = /o?/.test('google');                                                       //>1=0
res = /o{2}/.test('goooogle');                                                      //true
res = /o{1,3}/.test('goooogle');                                                     //1到多少
res = /^k/.test('king');                                                          //k開頭的
res = /i$/.test('mai');                                                          //i結尾的
res = /o(?=w)/.test('helloworld');                                                      //后面緊給的 全局
res = /o(?!w)/.test('helloworld');                                                    //后面不是w的 全局
res = /\d/.test('aajkldsfj');                                                       //[0-9] //false
res = /\D/.test('sdfkjllsdfj');                                                       //[^0-9] //非數字的
console.log(res);

//exec方法

除了數組元素和 length 屬性之外,exec() 方法還返回兩個屬性。index 屬性聲明的是匹配文本的第一個字符的位置。input 屬性則存放的是被檢索的字符串 string。

Array(1)0: "is"groups: undefinedindex: 2input: "this is a test"length: 1__proto__: Array(0)


res = /is/i.exec('this is a test');
console.log(res);
console.log(res[0]);

當 RegExpObject 是一個全局正則表達式時,exec() 的行為就稍微復雜一些。它會在 RegExpObject 的 lastIndex 屬性指定的字符處開始檢索字符串 string。找到i從i的最后一個字符后面開始查找                                                     //is
var str = 'this is a test hello nana hello world';
var patt = /i/ig;
var myArr;
while((myArr = patt.exec(str)) !== null) {
var msg = '找到了' + myArr[0] + '!';
msg += '下一個匹配從' + patt.lastIndex;                                     //找到i從i的最后一個字符后面開始查找
console.log(msg);
}


var str = 'this is a test';
res = str.match(/IS/i);                                                     //只查詢一次
console.log(res);


res = str.match(/IS/ig);                                                   //兩個數組
console.log(res);


res = str.search(/is/i);                                                  //從幾開始
console.log(res);


var str1 = str.replace(/is/ig, '!');                                             //替換成感嘆號
console.log(str1);


var str = '2015-09-27';
res = str.replace(/(\d{4})-(\d{2})-(\d{2})/, '$2/$3/$1');
console.log(res);


str = 'Hello nana Edu';
res = str.replace(/[A-Z]/g, func);                                            //輸出結果變為kinghello
function func(match) {
return 'king_' + match.toLowerCase();
}
console.log(res);


res = str.split("");                                                    //分割
console.log(res);

 

 

 

什么是數組?


數組對象的作用是:使用單獨的變量名來存儲一系列的值。

//數組直接量(字面量)形式創建數組
var arr=[];                                                  //空數組
var arr1=[1,2,3,4,5,6];
var arr2=[1,2.3,true,false,null,undefined,[1,2,3],{x:1,y:2}];                                                                                       //可以放很多數據  包括對象和數組
var x=1;
var arr3=[x,x*3,x+2,x+3];                                                            //輸出結果為1.3.1.4
console.log(arr3);
var arr4=[1,,3];                                          //[1,undefined,3]
console.log(arr4[1]);                                          //undenfiend
console.log(arr4.length);                                         //3
var arr5=[,,];                                             //逗號后面是算長度的
console.log(arr5.length);                                         //2
//通過構造函數Array()創建數組
var a=new Array();//[]
var a=new Array(5);                                                                 //控制長度
console.log(a.length);
var a=new Array(1,2,'king',false,2.3,null);                                  //[1,2,'king',false,2.3,null]返回原來的
console.log(a);

數組長度值為4,但查看為0開始
var arr=[1,2,3,4];
console.log(arr[0]);                                              //1
console.log(arr[3]); 

                                             //4
arr[0]='king';                                                //1變為King
console.log(arr);


arr[5]='hello nana';                                                     //添加hellonana
console.log(arr);


var x=2;                                                   //king 2 3 4 hello nn
console.log(arr[x]);                                                   //結果為3
arr[arr[x]]=333;                                                 //king 2 3 333 hello nn  //把4換為3333
console.log(arr);


var obj={x:"2",y:"4"};
obj[0]='a';
obj[1]='b';
console.log(obj);
console.log(obj.length);                                              //對象沒有length屬性

var arr=[];
arr[0]='a';
arr[1]='b';                                                    //只能讀取到2個
arr[-123]='c';
arr[2.3]='d';
arr[null]='e';
arr[false]='f';
arr[undefined]='g';
arr['name']='h';
arr[4]="pp";
console.log(arr);                                                     //長度為5
//console.log(arr.length);


var arr=new Array(10);
arr=[];                                                            //覆蓋為空 為0
arr[100]='abc';
console.log(arr.length);                                                     //101長度

var arr=[1,2,3,4,5,6,7,8];
arr.length=3;
console.log(arr);                                                       //1.2.3

arr.length=0;
console.log(arr);                                                       //0

arr.length=5;
console.log(arr);                                                    //empty*5

var arr=[1,2,3];
Object.defineProperty(arr,'length',{                                                 //不可改變輸出原來的
writable:false
});

console.log(arr.length);


arr.length=10;
console.log(arr.length);                                                  //數組還是3

如何刪除數組開頭結尾,添加等

var arr=[];
arr[0]='a';
arr[1]='b';
arr.shift();                                                        //開頭刪除
arr.push('d','e','f',12,34);                                                    //尾部添加
console.log(arr);


var res=arr.pop();                                                       //尾部刪除
console.log(res);
// console.log(arr);
arr.unshift(1,2,3,4,5);                                                     //開頭添加
console.log(arr);



res=arr.shift();
console.log(res);
console.log(arr);

var arr=['a','b','c','d'];
delete arr[0];                                                        //刪除后位置保留  長度還是為4
console.log(arr);
console.log(arr.length);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM