Javascript Web前端之路让前端飞

正则表达式面试题

2017-05-06  本文已影响1659人  打铁大师

正则表达式教程点击这里

例1

请使用正则取出:Tech,Sales,内容1,内容2
要求最好返回值是一个二维数组:如:a[0][0] = ‘Tech’;a[0][1] = ‘Sales’;

let str = 'Tech,Sales,内容1,内容2';
let regExp = /Tech|Sales|内容1|内容2/g;
let myArray;
let array = [
    []
];
while ((myArray = regExp.exec(str)) !== null) {
    array[0].push(myArray[0]);
}
console.log(array);

例2

<OPTION value="待处理">待处理</OPTION>
写一个正则表达式,匹配 "<OPTION value="待处理">"

let str = '<OPTION  value="待处理">待处理</OPTION>';
let regExp = /^<.*?>/;
console.log(regExp.exec(str)[0]); 
结果//<OPTION  value="待处理">

例3

如何获取一个字符串中的数字字符,并按数组形式输出,如
dgfhfgh254bhku289fgdhdy675gfh输出[254,289,675]

let str = 'dgfhfgh254bhku289fgdhdy675gfh';
let regExp = /\d+/g;
console.log(str.match(regExp));

例4

敏感词过滤
比如:“我草你妈哈哈背景天胡景涛哪肉涯剪短发欲望”
过滤:‘草肉欲胡景涛’

let str = '我草你妈哈哈背景天胡景涛哪肉涯剪短发欲望';
let regExp = /草|肉|欲|胡景涛/g;
let result = str.replace(regExp, function(match) {
    let len = match.length;
    let str;
   switch (len) {
    case 1:
          str = '*';
          break;
      case 2:
          str = "**";
          break;
      case 3:
           str = "***";
          break;
      default:
          str = '****';
  }
    return str;
});
console.log(result); //我*你妈哈哈背景天***哪*涯剪短发*望

例5

让2013-6-7 变成 2013.6.7

let str = '2013-6-7';
let regExp = /-/g;
console.log(str.replace(regExp, '.')); //2013-6-7

例6

写出正则表达式, 从一个字符串中提取链接地址。 比如下面字符串中

 "IT面试题博客中包含很多  <a href='http://hi.baidu.com/mianshiti/blog/category/微软面试题'> 微软面试题 </a> "

let str = 'IT面试题博客中包含很多  <a href="http://hi.baidu.com/mianshiti/blog/category/微软面试题">微软面试题</a>';
let regExp = /<a(?: [^>]*)+href="(.*)"(?: [^>]*)*>/;
console.log(regExp.exec(str)[1]);
//http://hi.baidu.com/mianshiti/blog/category/微软面试题

例7

var s1 = "get-element-by-id";
给定这样一个连字符串,写一个function转换为驼峰命名法形式的字符串 getElementById

  var s1 = "get-element-by-id";

function camelCased(str) {
  let regExp = /-(\w)/g;
  str.replace(regExp, function(match, p) {
      return p.toUpperCase();
   })
}
camelCased(s1);

例8

判断字符串是否包含数字

let str1 = 'abc9efh';
let str2 = 'abcefg';
let regExp = /\d/;
console.log(regExp.test(str1)); // true
console.log(regExp.test(str2)); // false

例9

判断连续重复字母

let str1 = 'abc3d4e5';
let str2 = 'aab2c3';
let regExp = /([a-zA-Z])\1/;
console.log(regExp.test(str1));//false
console.log(regExp.test(str2));//true

例10

判断是否以元音字母结尾

let str1= 'animal';
let str2 = 'li';
let str3 = 'foO';
let regExp = /[a,e,i,o,u]$/i;
console.log(regExp.test(str1)); //false
console.log(regExp.test(str2)); //true
console.log(regExp.test(str3)); //true

例11

给定字符串 str,检查其是否包含 3 个连续的数字
1、如果包含,返回最新出现的 3 个数字的字符串
2、如果不包含,返回 false

let str1 = 'abc123efg';
function captureThreeNumbers(str) {
    let res;
    if (res = str.match(/\d{3}/)) {
        return res[0];
    } else {
        return false;
    }
}
console.log(captureThreeNumbers(str1)); //123

例12

判断是否符合指定格式;
给定字符串 str,检查其是否符合如下格式
1、XXX-XXX-XXXX
2、其中 X 为 Number 类型

let regExp =  /^(\d{3}-){2}\d{4}$/;
console.log(regExp.test('123-456-7890'));

例13

判断是否符合 USD 格式:

给定字符串 str,检查其是否符合美元书写格式
1、以 $ 开始
2、整数部分,从个位起,满 3 个数字用 , 分隔
3、如果为小数,则小数部分长度为 2
4、正确的格式如:$1,023,032.03 或者 $2.03,错误的格式如:$3,432,12.12 或者 $34,344.3

let regExp = /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/;
console.log(regExp.test('$1.23')); //true
console.log(regExp.test('$111.23')); //true 
console.log(regExp.test('$1111.23')); //false
console.log(regExp.test('$1,123.23')); //true

例14

获取 url 中的参数

  1. 指定参数名称,返回该参数的值 或者 空字符串

  2. 不指定参数名称,返回全部的参数对象 或者 {}

  3. 如果存在多个同名参数,则返回数组

     //    获取 url 参数
     function getUrlParam(sUrl, sKey) {
       var arr={};
       sUrl.replace(/\??(\w+)=(\w+)&?/g,function(match,p1,p2){
           //console.log(match,p1,p2);
           if(!arr[p1]){
               arr[p1]=p2;
           }
           else {
               var p=arr[p1];
               arr[p1]=[].concat(p,p2);
           }
       })
       if(!sKey)return arr;
       else{
           for(var ele in arr){
               if(ele==sKey){return arr[ele];}
           }
           return "";
       }
     }
    

例15

对人口数字的格式化处理,三位数字用一个','(逗号)隔开

function numberWithCommas(x) {
    //对右侧人口数字的格式化处理,三位数字用一个','(逗号)隔开
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
console.log(numberWithCommas(12345678))//12,345,678

例16

将单词is替换为IS

let str = 'English poetry is one of their great heritages';
console.log(str.replace(/\bis\b/,'IS'));
// English poetry IS one of their great heritages

例17

去掉http协议的jpg文件的协议头

let imgs = [
  'http://img.host.com/images/fds.jpg',
    'https://img.host.com/images/fjlj.jpg',
    'http://img.host.com/images/djalsdf.png',
    'https://img.host.com/images/adsjfl.png',
    'http://img.host.com/image/jasdlf.jpg'];
    imgs = imgs.map((img)=>{
       return img.replace(/http:(\/\/.+\.jpg)/,function(match,p1){
          return p1
      });
});
console.log(imgs);
// output [ '//img.host.com/images/fds.jpg',
  'https://img.host.com/images/fjlj.jpg',
  'http://img.host.com/images/djalsdf.png',
  'https://img.host.com/images/adsjfl.png',
  '//img.host.com/image/jasdlf.jpg' ]

例18

找出数组中的表示日期的时间字符串,并修改格式为‘月-日-年’

let times= ['2006/02/03',
  'test/07/sd',
  '2016/05/10',
  '1998-03-07',
  '12345/23/45678',
  '1234/23/56789',
  '12345/23/45']
  times = times.map((time)=>{
    return time.replace(/^(\d{4})[/-](\d{2})[/-](\d{2})$/,(match,p1,p2,p3)=>{
    return `${p2}-${p3}-${p1}`
  })
});
console.log(times);
//[ '02-03-2006',
  'test/07/sd',
  '05-10-2016',
  '03-07-1998',
  '12345/23/45678',
  '1234/23/56789',
  '12345/23/45' ]
上一篇下一篇

猜你喜欢

热点阅读