string.replace正则表达式说明
str.replace(reg,function($0,$1,$2...,index,str){
});
$0: 匹配模式的字符串
$1...: 匹配模式子表达式的字符串,0个或多个,个数取决于子表达式的个数
index: $0在原字符串中的位置
str: 原字符串
利用此可以写出简洁的方法
例如:
1. format方法
function format(s,arg0){
var args = arguments;
return s.replace(/\{(\d+)\}/,function($0,$1){
return args[($1 | 0) + 1] || '';
})
}
format("{0} love {1}.",'I','You')//I love you
2.交换相邻单词
function fSwapWord(){
var ss = "The rain in Spain falls mainly in the plain.";
var r = ss.replace(/(\S+)(\s+)(\S+)/g, "$3$2$1"); // 交换每一对单词。
return(r); // 返回结果字符串。
}
结果:rain The Spain in mainly falls the in plain
string.replace正则表达式说明的更多相关文章
- python 字符串替换功能 string.replace()可以用正则表达式,更优雅
说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...
- 基础笔记3(二)(专门处理String的正则表达式)
1.常规判断一个字符串是以什么开头,是否是数字的判断方式有: a.通过比较每个字符,注意比较是字符值(ASc码值),不是字面值 String s="); //判断每个字符数组的每个字符 ch ...
- String.replace与String.format
字符串的替换函数replace平常使用的频率非常高,format函数通常用来填补占位符.下面简单总结一下这两个函数的用法. 一.String.replace的两种用法 replace的用法如:repl ...
- [转]String.Replace 和 String.ReplaceAll 的区别
JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...
- JAVA中string.replace()和string.replaceAll()的区别及用法
乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样. public String r ...
- Java String.replace()方法
Java String.replace()方法用法实例教程, 返回一个新的字符串,用newChar替换此字符串中出现的所有oldChar 声明 以下是java.lang.String.replace( ...
- .net 基础错误-string.replace 方法
1.string string.Replace(string oldValue,string newValue) 返回一个新的字符串,其中当前示例中出现的所有指定字符串都替换另一个指定字符串 错误:总 ...
- Python string replace 方法
Python string replace 方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...
- [Algo] 649. String Replace (basic)
Given an original string input, and two strings S and T, replace all occurrences of S in input with ...
随机推荐
- Servlet、Filter和Listener
Java Servlet是与平台无关的服务器端组件,运行于Servlet容器中(如Tomcat),Servlet容器负责Servlet和客户端的通信以及调用Servlet的方法,Servlet和客户端 ...
- jquery datatable
<html><head></head> <script type="text/javascript"> $(document).re ...
- exit(0)、exit(1)、exit(-1)的区别
exit(0) - 正常退出 exit(1) - 异常退出(除0外,其他值均为异常退出)
- 关于IOS时间日历的一些注意事项 NSDateFormatter
1.时间或者日期格式化的格式化,在真机上运行的是hi,必须指定是在哪个区域的,不然在真机无法显示 // 1.日期格式化 NSDateFormatter *fmt = [[NSDateFormatter ...
- Python 编程小备忘
1. 获取当前日期,或者间隔当前任意天的日期. >>> import datetime>>> print (datetime.date.today()-dateti ...
- IIS提示“异常详细信息: System.Runtime.InteropServices.ExternalException: 无法执行程序”
先来看错误提示: 无法执行程序.所执行的命令为 "C:/Windows/Microsoft.NET/Framework/v3.5/csc.exe" /noconfig /fullp ...
- CodeForces 51F Caterpillar
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- win7下firefox浏览器不能使用
win7下firefox浏览器不能使用,只有360浏览器才能使用. 使用360安全卫士,到更多工具里面选择"LSP修复",就可以了. 原来是因为安装了土豆加速,然后卸载导致的. 看 ...
- QCustomPlot 使用整理
QCustomPlot 是一个比较小的 QT 图表插件.使用时,我们在程序中写完相关调用的代码后,只需将 QCunstomPlot.cpp 和 QCustomPlot.h 两个文件加入工程,正常编译即 ...
- SQL请求优化——请求次数统计,SQL写操作稀释
引言 前几天做了这么一个东西:一个游戏中有个活动页面,活动页面有个商品,商品下面要显示该商品实浏览次数,就相当于是用户每出发一次请求这个浏览总次数都会添加一次,这个问题很简单,每次浏览的时候去数据库中 ...