Home  >  Article  >  Web Front-end  >  web.js.String and regular expression operation example tutorial

web.js.String and regular expression operation example tutorial

零下一度
零下一度Original
2017-05-15 09:59:441053browse

This article mainly introduces relevant information about web.js. string and regular expression operations. Friends in need can refer to

1.substring

var str='abcdef';
alert(str.substring(2, 5)); //cde不包括结束位置
alert(str.substring(1));//bcdef1

2.split

var str='a*b*cd*ef';
alert(str.split('*'));//分割字符1

3.search

var str='acef'; 
alert(str.search(‘a'));//0查找字符位置 
alert(str.search(‘f'));//3 
 alert(str.search(‘ce'));//1 
 alert(str.search(‘o'));//-1匹配失败则-1

regular

var re=new RegExp('b', 'i'); //i不考虑大小写
//或者var re=/b/i;
var str='abcdef';//将b换成B同样的结果,如果去掉i就不行了
alert(str.search(re));

1.match

var str='asdf 34 656 cvs33';
var re=/\d/g;
alert(str.match(re));//3,4,6,5,6,3,3match 获取匹配的项目1
var str='asdf 34 656 cvs33';
var re=/\d+/g;//全局匹配:g——global,+表示一次或者多次
alert(str.match(re));//34,656,33

2.replace

var str='asdf 34 656 cvs33';
var re=/\d+/g;
var re2=/\d/g;
alert(str.replace(re,'*'));//asdf * * cvs*;
alert(str.replace(re2,'*'));//asdf ** *** cvs**1

Remove sensitive words

var str='河南 一村民 开封 哈哈'
  var re=/河南|开封/g;//去掉敏感词河南或开封
  var re1=/河南|开封/;
  alert(str.replace(re,'*'));
  alert(str.replace(re1,'*'))//没有去掉开封,自己试试结果1

3 .[] Any character, range

[abc]

Example: o[usb]t——obt, ost, out

[a-z ], [0-9]

Example: id[0-9]——id0, id5

[^a](exclude everything except a)

Example: o[^0-9]t——oat, o?t, o t

combination

[a-z0-9A-Z]

【Related Recommendations】

1. Special Recommendation:"php Programmer Toolbox" V0.1 version download

2. Free js online video tutorial

##3.

php.cn Dugu Jiujian (3) - JavaScript video tutorial

The above is the detailed content of web.js.String and regular expression operation example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn