zoukankan      html  css  js  c++  java
  • js match方法的使用

     

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

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

    语法

     
     
     
    x
     
     
     
     
    1
    stringObject.match(searchvalue)//searchvalue检索的字符串的值
    2
    stringObject.match(regexp)//regexp正则表达式
     
     

    返回值

    存放匹配结果的数组。

    检索的两种方式

    1. 字符串

       
       
       
      xxxxxxxxxx
      5
       
       
       
       
      1
      var str2="Hello World!,Hello World!";
      2
          console.log(str2.match("World"));   //["World", index: 6, input: "Hello World!,Hello World!", groups: undefined]
      3
          console.log(str2.match("World").input); //Hello World!,Hello World!
      4
          document.write(str2.match("World"));    //World
      5
          document.write(str2.match("world")) //null
       
       
    2. 正则表达式

       
       
       
      x
       
       
       
       
      1
      var str3="1 plus 2 equal 3"
      2
          //用正则表达式匹配数字
      3
          document.write(str3.match(/d+/g)); //1,2,3
      4
          console.log(str3.match(/d+/g));    //["1", "2", "3"]
       
       
  • 相关阅读:
    groovy execute
    forwarding
    C++实用技巧(一) λcalculus(惊愕到手了欧耶,GetBlogPostIds.aspx) C++博客
    lua与c若干问题 专职C++ C++博客
    [精华] kingate代理服务器指南
    Linux下的静态库和动态库 yg2362 C++博客
    069 问,lua程序设计(第四部分)笔记三,c++调用lua函数 everettjf C++博客
    partition list Groovy builtin to split an array into equal sized subarrays? Stack Overflow
    λcalculus(惊愕到手了欧耶,GetBlogPostIds.aspx) C++博客
    GDB调试使用技巧 专职C++ C++博客
  • 原文地址:https://www.cnblogs.com/nieaojie625/p/13235820.html
Copyright © 2011-2022 走看看