当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS常见疑难点分析之match,charAt,charCodeAt,map,search用法分析

JS常见疑难点分析之match,charAt,charCodeAt,map,search用法分析

2019年04月03日  | 移动技术网IT编程  | 我要评论

本文实例讲述了js常见疑难点分析之match,charat,charcodeat,map,search用法。分享给大家供大家参考,具体如下:

javascript match() 方法

定义和用法

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

该方法类似 indexof() 和 lastindexof(),但是它返回指定的值,而不是字符串的位置。

语法

匹配字符串,返回指定的值

stringobject.match(searchvalue)

匹配正则,返回指定的值

stringobject.match(regexp)

使用 match() 来检索一个字符串例子:

<html>
<body>
<script type="text/javascript">
var str="hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("world") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))
</script>
</body>
</html>

最终出现的结果为,world,null,null,world!

使用 match() 来检索一个正则表达式的匹配例子:

<html>
<body>
<script type="text/javascript">
var str="1 plus 2 equal 3";
//这里的正则表达式必须加上g,全局匹配,不然就会匹配一个值即返回
document.write(str.match(/\d+/g))
</script>
</body>
</html>

通常来说,我们用match用在正则上面比较多,也可以用其来代理indexof和lastindexof来判断字符串里面是否存在此值。

javascript search() 方法

定义和用法

search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,检索到则返回匹配的子串的起始位置,无法检索到值,返回-1。

语法

stringobject.search(regexp)

该参数可以是需要在 stringobject 中检索的子串,也可以是需要检索的 regexp 对象。

要执行忽略大小写的检索,请追加标志 i。

search() 例子:

<script type="text/javascript">
var str="visit w3school!"
document.write(str.search(/w3school/))
</script>

返回索引值为6,search通常与正则配合使用,可以达到indexof的效果。

javascript charat() 方法

定义和用法

charat() 方法可返回指定位置的字符。

请注意,javascript 并没有一种有别于字符串类型的字符数据类型,所以返回的字符是长度为 1 的字符串。

语法

返回指定位置的字符串

stringobject.charat(index)

chartat实例:

<script type="text/javascript">
var str="hello world!"
document.write(str.charat(1))
</script>

最终返回结果为:e,通常我们可以通过chartat来从某个字符串取得具体的字符。

javascript charcodeat() 方法

定义和用法

charcodeat() 方法可返回指定位置的字符的 unicode 编码。这个返回值是 0 - 65535 之间的整数。

方法 charcodeat() 与 charat() 方法执行的操作相似,只不过前者返回的是位于指定位置的字符的编码,而后者返回的是字符子串。

语法

stringobject.charcodeat(index)

charcodeat()实例

注释:字符串中第一个字符的下标是 0。如果 index 是负数,或大于等于字符串的长度,则 charcodeat() 返回 nan。

<script type="text/javascript">
var str="hello world!"
document.write(str.charcodeat(1))
//返回h的unicode 编码101
</script>

js中array.prototype.map()方法

定义和用法

map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。

语法

array.map(callback[, thisarg])

callback原数组中的元素经过该方法后返回一个新的元素。
currentvalue,callback 的第一个参数,数组中当前被传递的元素。
index,callback 的第二个参数,数组中当前被传递的元素的索引。
array,callback 的第三个参数,调用 map 方法的数组。
thisarg执行 callback 函数时 this 指向的对象。

map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数。callback 每次执行后的返回值组合起来形成一个新数组。 callback 函数只会在有值的索引上被调用;那些从来没被赋过值或者使用

delete 删除的索引则不会被调用。callback 函数会被自动传入三个参数:数组元素,元素索引,原数组本身。

使用map()的第一个例子:

下面的代码将一个数组中的所有单词转换成对应的复数形式.

function fuzzyplural(single) {
 var result = single.replace(/o/g, 'e');
 if( single === 'kangaroo'){
  result += 'se';
 }
 return result;
}
var words = ["foot", "goose", "moose", "kangaroo"];
console.log(words.map(fuzzyplural));

最后结果:

["feet", "geese", "meese", "kangareese"]

求数组中每个元素的平方根例子

var numbers = [1, 4, 9];
var roots = numbers.map(math.sqrt);
/* roots的值为[1, 2, 3], numbers的值仍为[1, 4, 9] */

在字符串上使用 map 方法

var map = array.prototype.map
var a = map.call("hello world", function(x) { return x.charcodeat(0); })
// a的值为[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

<!doctype html>
<html lang="en-us">
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  <script type="text/javascript">
    //var map = array.prototype.map
    var a = array.prototype.map.call("hello world", function(x) { return x.charcodeat(0); })
    // a的值为[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
    alert(a);
  </script>
</body>
</html>

map兼容旧环境

map 是在最近的 ecma-262 标准中新添加的方法;所以一些旧版本的浏览器可能没有实现该方法。在那些没有原生支持 map 方法的浏览器中,你可以使用下面的 javascript 代码来实现它。所使用的算法正是 ecma-262,第 5 版规定的。假定object, typeerror, 和 array 有他们的原始值。而且 callback.call 的原始值也是 function.prototype.call。

// 实现 ecma-262, edition 5, 15.4.4.19
// 参考: http://es5.github.com/#x15.4.4.19
if (!array.prototype.map) {
 array.prototype.map = function(callback, thisarg) {
  var t, a, k;
  if (this == null) {
   throw new typeerror(" this is null or not defined");
  }
  // 1. 将o赋值为调用map方法的数组.
  var o = object(this);
  // 2.将len赋值为数组o的长度.
  var len = o.length >>> 0;
  // 4.如果callback不是函数,则抛出typeerror异常.
  if ({}.tostring.call(callback) != "[object function]") {
   throw new typeerror(callback + " is not a function");
  }
  // 5. 如果参数thisarg有值,则将t赋值为thisarg;否则t为undefined.
  if (thisarg) {
   t = thisarg;
  }
  // 6. 创建新数组a,长度为原数组o长度len
  a = new array(len);
  // 7. 将k赋值为0
  k = 0;
  // 8. 当 k < len 时,执行循环.
  while(k < len) {
   var kvalue, mappedvalue;
   //遍历o,k为原数组索引
   if (k in o) {
    //kvalue为索引k对应的值.
    kvalue = o[ k ];
    // 执行callback,this指向t,参数有三个.分别是kvalue:值,k:索引,o:原数组.
    mappedvalue = callback.call(t, kvalue, k, o);
    // 返回值添加到新书组a中.
    a[ k ] = mappedvalue;
   }
   // k自增1
   k++;
  }
  // 9. 返回新数组a
  return a;
 };
}

通常生成时间戳的巧妙方法

第一种方式

function gettimestamp()
{
var timestamp=new date().gettime();
var timestampstring = timestamp.tostring();//一定要转换字符串
oldtimestamp = timestampstring;
return timestampstring;
}

第二种方式

new date().tostring() //同样可以达到效果,更简洁

如何使用md5加密方法:

引用google,md5加密的库文件:http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js

其实蛮简单的,里面cryptojs.sha1(),直接引用加密即可,举个栗子:

就这样直接调用就可以了

var keyvaluestring = "ddddd";
sign = cryptojs.sha1(keyvaluestring).tostring();

ps:这里再提供几款相关的加密与正则工具供大家参考使用:

javascript正则表达式在线测试工具:

正则表达式在线生成工具:

base64编码解码工具:

url网址16进制加密工具:

md5在线加密工具:

在线散列/哈希算法加密工具:

更多关于javascript相关内容可查看本站专题:《javascript常用函数技巧汇总》、《javascript面向对象入门教程》、《javascript中json操作技巧总结》、《javascript切换特效与技巧总结》、《javascript查找算法技巧总结》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网