博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript中的正则表达式
阅读量:3986 次
发布时间:2019-05-24

本文共 1722 字,大约阅读时间需要 5 分钟。

修饰符:i	执行对大小写不敏感的匹配。g	执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。m	执行多行匹配。

一、javascript字符串提供的功能:

var str = 'http://localhost:8089/dist/index.html#/set_product';
1、match(regexp: string | RegExp): RegExpMatchArray | null;/*** Matches a string with a regular expression, and returns an array containing the results of that search.* @param regexp A variable name or string literal containing the regular expression pattern and flags.*/str.match('/^http/i');var e = str.match('/^https/i') ? "https://" : "http://";2、search(regexp: string | RegExp): number/*** Finds the first substring match in a regular expression search.* @param regexp The regular expression pattern and applicable flags.*/用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,并返回子串的起始位置。str.search('/^http/i');如果没有匹配到则返回 -13、replace(searchValue: string | RegExp, replaceValue: string): string;/*** Replaces text in a string, using a regular expression or search string.* @param searchValue A string to search for.* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.*/用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。str.replace('/login_page', '/login');

二、使用 RegExp 对象

exec 方法 /*** Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.* @param string The String object or string literal on which to perform the search.*/exec(string: string): RegExpExecArray | null;test 方法 /*** Returns a Boolean value that indicates whether or not a pattern exists in a searched string.* @param string String on which to perform the search.*/test(string: string): boolean;查找路由,期望得到的结果 /set_productvar patt = /index.html#(.*)/;var mat = patt.exec(str);str.replace(mat[1], '/login');

转载地址:http://pjaui.baihongyu.com/

你可能感兴趣的文章
Qt5 mouseless 测试
查看>>
qt5 mouseless 多种部件切换带焦点高亮显示
查看>>
udev和mdev hotplug事件
查看>>
And that’s what the lowest level of the Libevent API does for you
查看>>
webbench源码分析
查看>>
httpd源码分析
查看>>
192.168.1.1/24 什么含义
查看>>
输入设备节点自动生成
查看>>
dynamic generate command line parameters for qt embedded application
查看>>
opencv test code-1
查看>>
烟台地铁路线图 Yantai Subway
查看>>
eclipse 导入先前存在的项目
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
imx6 项目的按键驱动程序
查看>>
busybox passwd修改密码
查看>>
wpa_supplicant控制脚本
查看>>
rfkill: WLAN hard blocked
查看>>
gstreamer相关工具集合
查看>>
gstreamer 捕获图像+存储示例代码
查看>>