JavaScript RegExp Object
来源:互联网 发布:mac官网彩妆 编辑:程序博客网 时间:2024/05/16 19:37
RegExp, is short for regular expression.
Complete RegExp Object Reference
For a complete reference of all the properties and methods that can be used with the RegExp object, go to our complete RegExp object reference.
The reference contains a brief description and examples of use for each property and method!
What is RegExp?
A regular expression is an object that describes a pattern of characters.
When you search in a text, you can use a pattern to describe what you are searching for.
A simple pattern can be one single character.
A more complicated pattern can consist of more characters, and can be used for parsing, format checking, substitution and more.
Regular expressions are used to perform powerful pattern-matching and "search-and-replace" functions on text.
Syntax
var patt=new RegExp(pattern,modifiers);or more simply:
var patt=/pattern/modifiers;
- pattern specifies the pattern of an expression
- modifiers specify if a search should be global, case-sensitive, etc.
RegExp Modifiers
Modifiers are used to perform case-insensitive and global searches.
The i modifier is used to perform case-insensitive matching.
The g modifier is used to perform a global match (find all matches rather than stopping after the first match).
Example 1
Do a case-insensitive search for "w3schools" in a string:
var str="Visit W3Schools";var patt1=/w3schools/i;
The marked text below shows where the expression gets a match:
Visit W3SchoolsTry it yourself »
Example 2
Do a global search for "is":
var str="Is this all there is?";var patt1=/is/g;
The marked text below shows where the expression gets a match:
Is this all there is?Try it yourself »
Example 3
Do a global, case-insensitive search for "is":
var str="Is this all there is?";var patt1=/is/gi;
The marked text below shows where the expression gets a match:
Is this all there is?Try it yourself »
test()
The test() method searches a string for a specified value, and returns true or false, depending on the result.
The following example searches a string for the character "e":
Example
var patt1=new RegExp("e");document.write(patt1.test("The best things in life are free"));
Since there is an "e" in the string, the output of the code above will be:
trueTry it yourself »
exec()
The exec() method searches a string for a specified value, and returns the text of the found value. If no match is found, it returns null.
The following example searches a string for the character "e":
Example 1
var patt1=new RegExp("e");document.write(patt1.exec("The best things in life are free"));
Since there is an "e" in the string, the output of the code above will be:
eTry it yourself »
- JavaScript RegExp Object
- RegExp Object
- Javascript --RegExp
- JavaScript RegExp
- JavaScript RegExp
- RegExp (regular expression) object
- js 数据类型 Array,Object,RegExp
- JavaScript RegExp 对象参考
- JavaScript RegExp 对象
- JavaScript RegExp 对象
- JavaScript replace new RegExp
- JavaScript的RegExp对象
- javascript RegExp replace
- JavaScript的RegExp
- JavaScript RegExp 对象参考手册
- javascript之RegExp
- JavaScript的RegExp对象
- javaScript RegExp对象问题
- [模拟器问题] 关于ActivityManager: Error: Activity class {..} does not exist. 的解决
- alkacon oamp comments的使用
- C语言中,strstr函数的功能
- 马云经典语录
- 怎样做好项目计划
- JavaScript RegExp Object
- Sqlserver数据库表重设置自增主键ID号
- 浮动元素的两端对齐
- symbian的加载dll出现错误
- Oracle Tips2( Getting Started with orcale server)
- Oracle Tips 3 (Managing an Oracle Instance)
- 马士兵老师对struts2的详解
- X-Plane
- X-Plane 9 极速入门