zoukankan      html  css  js  c++  java
  • Node.js之判断字符串中是否包含某个字符串

    server.txt内容如下:

    阿里云服务器

    关于应用场景,就不多说了,字符串是不论是后端开发还是前端开发等,都是要经常打交道了。

    test.js(node.js代码,只要被本地装了node.js环境,直接可通过node test.js运行看效果):

    var fs = require("fs");
    
    var result = fs.readFileSync("./server.txt");
    
    console.log("result:"+result);
    
    if(result.indexOf("阿里云服务器") != -1){
        
        console.log("ok");
    } else{
        
        console.log("no");
        
    }
    //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。
    
    
    if(result.toString().search("1") != -1){
        console.log("ok");
        
    }else{
        console.log("no");
    }
    //search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。如果没有找到任何匹配的子串,则返回 -1。
    
    
    
    var reg = RegExp(/阿里云/);
    if(result.toString().match(reg)){
        console.log("ok");      
    }else{
        console.log("no");
        
    }
    //match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
    
    
    
    var reg = RegExp(/阿里云/);
    if(reg.test(result)){
        
        console.log("ok");
    }else{
        console.log("no");
        
    }
    
    //test() 方法用于检索字符串中指定的值。返回 true 或 false。
    
    
    
    var reg = RegExp(/阿里云/);
    if(reg.exec(result)){    
           console.log("ok");      
    }else{
        console.log("no");
    }
    //exec() 方法用于检索字符串中的正则表达式的匹配。返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。

    本文主要参考资料如下:
    js 判断字符串中是否包含某个字符串

  • 相关阅读:
    redis-单线程为什么快
    redis-数据结构
    http-状态码
    事件绑定完整版2016/4/21
    焦点事件2016、4、21
    ++
    Bom2016/4/21
    添加以及删除className
    getByClassName2016/4/21
    动态添加
  • 原文地址:https://www.cnblogs.com/youcong/p/11209791.html
Copyright © 2011-2022 走看看