String objects and regular expressions

1, String string, and there are differences, is a double or single quotes up data comprises character array is read-only string, the other array is essentially composed of a plurality of strings;

2, you say that he is an array of string, he's still a little difference, and arrays, and their similarities are all elements which can be accessed by subscript are supported API slice

 Difference is that the array can modify the original array, but an array of strings can not modify the original API string array can not, because the read-only string of principle, it can not be changed once created content

3.String object API

anchor () to create an HTML anchor

Big () with a large font display character string

blingk () flashing display string

Bold () is shown in bold character string

charAt () Returns the character in the designated seats

charCodeAt () Unicode encoding Returns the string position

concat () connection string

Fixed () displays a text string typewriter  

fontcolor () specified using the color to display the string

fontSize () using the specified size to display the string

formCharCode () to create a character from the character encoding

indexOf () to retrieve character

Italics () string using italics

lastIndexOf () search string from back to front

link () will appear as a link string

localeCompare () with a specific local order to compare two strings

match () to find one or more regular expressions to match

replace () Replace substring matching the regular expression

search () to retrieve the value of the regular expression match

slice () to extract pieces of the string, and return the extracted part in a new string

Small () using the small font to display the string

split () string is divided into an array of strings

Strike () to display the string strikethrough

sub () is displayed as a string subscript

() Starting from the specified index number of the extracted string of characters substr

Character string between two specified index number substring () to extract.

SUP () is displayed as a string superscript

toLocaleLowerCase () to convert strings to lower

toLocaleUpperCase () to convert a string to upper case

the toLowerCase () to convert strings to lower

toUpperCase () to convert a string to upper case

toString () Returns a string

Among many of these API and API array of the same, which is almost the same usage

4. Regular expressions also we usually more common

Regular expressions can be said to be the norm, the provisions of the law of a string of characters appearing in the expression

Syntax · [alternate character list]

eg: ex: [micro WV] [Letter X] of the matched content: wx vx micro-channel micro-channel xv letter w

In fact, among the regular characters may be omitted shorthand

Digit: [0123456789] ==> [0-9]


A lowercase letters: [az]


A capital letter: [AZ]


A letter: [a-zA-Z]
a Chinese character: [\ u4e00- \ u9fa5]

\ w an alphanumeric or _ [0-9a-zA-Z_]
\ D digit [0-9] digital (digital)
\ S a null character all invisible characters: Space, Tab, Enter. .
. an arbitrary character

Beginning of a string ^ ex: ^ \ s + null character at the beginning of the
end of the string $ ex: \ s + null character $ at the end of
a word boundary \ b ex: \ bno \ b matches no word, but the front and rear can be used punctuation or separated by spaces and other characters

eg: such as a telephone number

There mailbox

var str = "no one no two three no no now ";
str = str.replace(/(no)|(two)|(now)/g,//将str当中的字符no或two替换
kw=>kw.length==2?"**":"***");//ES6当中新出的箭头函数
//function(kw){//属于自定义函数
// return kw.length==2?"**":"***";
// });
console.log(str);
// str = str.replace(/no/ig,"不");
// console.log(str);

我们可以改变一些敏感的字符和找关键字如上

正则当中的格式化;

var birth="19980720";

birth  =brith.substr(0,4)+‘'年'+brith.substr(4,2)+‘月'+brith.substr(6)+‘'日'

birth  =birth.replace(/(\d{4})(\d{2})(\d{2})/,"$1年$2月$3日") ;//用replace按照新的格式进行分组

console.log(birth);

切割的例子:function nameSort(){

1.获取ulName元素当中的HTML的内容
document.getElementById("ulName").innerHTML ="<li>"+document.getElementById("ulName").innerHTML.replace(/^\s*<li>|<\/li>\s*$/g,"").split(/<\/li>\s*<li>/).sort().join("</li><li>")+"</li>";
2.删除html开头的空字符<li>和结尾的</li>的空字符;
 html = html.replace(/^\s*<li>|<\/li>\s*$/g,"");
3.按照html</li>空字符<li>切割html保存到names数组中
var names = html.split(/<\/li>\s*<li>/);
names.sort();//4.对数组进行排序
console.log(names);
5.按照拆分和进行还原
 html = "<li>"+//开头
 names.join("</li><li>")+"</li>";//结尾
 console.log(html);
 document.getElementById("ulName").innerHTML = html;
}
nameSort();

 正则的查找匹配

var msg = "吃葡萄不吐葡萄皮,不吃葡萄到吐葡萄皮";
reg = /(吃|吐)(葡萄)/g;
arr = reg.exec(msg);
console.log(arr);
arr = reg.exec(msg);
console.log(arr);
arr = reg.exec(msg);
console.log(arr.index);
// while((arr = reg.exec(msg))!=null){
// console.log(`在位置${arr["index"]}发现敏感词${arr[0]}`);
// }

动态生成的表达式;

var mess = "床前明月光,一行白鹭上青天,日照香炉生紫烟"// var names = ["明月","白鹭","青天","紫烟"];

 var nameReg = new RegExp(names.join("|"),"g");
var kws = mess.match(nameReg);
 console.log(kws);
 var reg = /^\d{6}$/;
 while(!reg.test(prompt("请输入密码:"))){
 alert("密码格式不符!请输入六位数的密码!");
 document.write("<h1>验证通过</h1>");
 }

 正则的贪婪及懒惰

var html = '<link rel="stylesheet" href="RegExp.htrml"><body><ul><li><a href="www.baidu.com">1</a></li></body>';
reg = /<a\s+[^>]*?href="([^"]*?)"/ig;
// //进行分组
while(reg.exec(html)!=null){
console.log(RegExp.$1);
//获取并找到打一个分组的内容
}

这相当与一种爬虫技术

 

Guess you like

Origin www.cnblogs.com/zhangli123/p/11294295.html