正则表达式面试题

lucky芬 

其他的题目 http://www.zixue.it/thread-10221-1-1.html

例1

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

 
  1. let str = 'Tech,Sales,内容1,内容2';

  2. let regExp = /Tech|Sales|内容1|内容2/g;

  3. let myArray;

  4. let array = [

  5. []

  6. ];

  7. while ((myArray = regExp.exec(str)) !== null) {

  8. array[0].push(myArray[0]);

  9. }

  10. console.log(array);

例2

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

 
  1. let str = '<OPTION value="待处理">待处理</OPTION>';

  2. let regExp = /^<.*?>/;

  3. console.log(regExp.exec(str)[0]);

  4. 结果//<OPTION value="待处理">

例3

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

 
  1. let str = 'dgfhfgh254bhku289fgdhdy675gfh';

  2. let regExp = /\d+/g;

  3. console.log(str.match(regExp));

例4

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

 
  1. let str = '我草你妈哈哈背景天胡景涛哪肉涯剪短发欲望';

  2. let regExp = /草|肉|欲|胡景涛/g;

  3. let result = str.replace(regExp, function(match) {

  4. let len = match.length;

  5. let str;

  6. switch (len) {

  7. case 1:

  8. str = '*';

  9. break;

  10. case 2:

  11. str = "**";

  12. break;

  13. case 3:

  14. str = "***";

  15. break;

  16. default:

  17. str = '****';

  18. }

  19. return str;

  20. });

  21. console.log(result); //我*你妈哈哈背景天***哪*涯剪短发*望

例5

让2013-6-7 变成 2013.6.7

 
  1. let str = '2013-6-7';

  2. let regExp = /-/g;

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

例6

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

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

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

  4. let regExp = /<a(?: [^>]*)+href="(.*)"(?: [^>]*)*>/;

  5. console.log(regExp.exec(str)[1]);

  6. //http://hi.baidu.com/mianshiti/blog/category/微软面试题

例7

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

var s1 = "get-element-by-id";
function camelCased(sName) {
    var re = /-(\w)/g;
    //如果字符串以 - 开头则将 - 删掉
    if(sName[0] == '-'){sName = sName.replace('-', '');}
 
    //将 “-字母”形式的字符串替换为 “大写字母”的形式
    sName = sName.replace(re, function($0, $1){
        return $1.toUpperCase();
    })
    return sName;
}
console.log(camelCased(s1));

 

例8

判断字符串是否包含数字

 
  1. let str1 = 'abc9efh';

  2. let str2 = 'abcefg';

  3. let regExp = /\d/;

  4. console.log(regExp.test(str1)); // true

  5. console.log(regExp.test(str2)); // false

例9

判断连续重复字母

 
  1. let str1 = 'abc3d4e5';

  2. let str2 = 'aab2c3';

  3. let regExp = /([a-zA-Z])\1/;

  4. console.log(regExp.test(str1));//false

  5. console.log(regExp.test(str2));//true

例10

判断是否以元音字母结尾

 
  1. let str1= 'animal';

  2. let str2 = 'li';

  3. let str3 = 'foO';

  4. let regExp = /[a,e,i,o,u]$/i;

  5. console.log(regExp.test(str1)); //false

  6. console.log(regExp.test(str2)); //true

  7. console.log(regExp.test(str3)); //true

例11

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

 
  1. let str1 = 'abc123efg';

  2. function captureThreeNumbers(str) {

  3. let res;

  4. if (res = str.match(/\d{3}/)) {

  5. return res[0];

  6. } else {

  7. return false;

  8. }

  9. }

  10. console.log(captureThreeNumbers(str1)); //123

例12

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

 
  1. let regExp = /^(\d{3}-){2}\d{4}$/;

  2. 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

 
  1. let regExp = /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/;

  2. console.log(regExp.test('$1.23')); //true

  3. console.log(regExp.test('$111.23')); //true

  4. console.log(regExp.test('$1111.23')); //false

  5. console.log(regExp.test('$1,123.23')); //true

例14

获取 url 中的参数

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

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

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

     
    1. // 获取 url 参数

    2. function getUrlParam(sUrl, sKey) {

    3. var arr={};

    4. sUrl.replace(/\??(\w+)=(\w+)&?/g,function(match,p1,p2){

    5. //console.log(match,p1,p2);

    6. if(!arr[p1]){

    7. arr[p1]=p2;

    8. }

    9. else {

    10. var p=arr[p1];

    11. arr[p1]=[].concat(p,p2);

    12. }

    13. })

    14. if(!sKey)return arr;

    15. else{

    16. for(var ele in arr){

    17. if(ele==sKey){return arr[ele];}

    18. }

    19. return "";

    20. }

    21. }

例15

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

 
  1. function numberWithCommas(x) {

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

  3. return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

  4. }

  5. console.log(numberWithCommas(12345678))//12,345,678

例16

将单词is替换为IS

 
  1. let str = 'English poetry is one of their great heritages';

  2. console.log(str.replace(/\bis\b/,'IS'));

  3. // English poetry IS one of their great heritages

例17

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

 
  1. let imgs = [

  2. 'http://img.host.com/images/fds.jpg',

  3. 'https://img.host.com/images/fjlj.jpg',

  4. 'http://img.host.com/images/djalsdf.png',

  5. 'https://img.host.com/images/adsjfl.png',

  6. 'http://img.host.com/image/jasdlf.jpg'];

  7. imgs = imgs.map((img)=>{

  8. return img.replace(/http:(\/\/.+\.jpg)/,function(match,p1){

  9. return p1

  10. });

  11. });

  12. console.log(imgs);

  13. // output [ '//img.host.com/images/fds.jpg',

  14. 'https://img.host.com/images/fjlj.jpg',

  15. 'http://img.host.com/images/djalsdf.png',

  16. 'https://img.host.com/images/adsjfl.png',

  17. '//img.host.com/image/jasdlf.jpg' ]

例18

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

 
  1. let times= ['2006/02/03',

  2. 'test/07/sd',

  3. '2016/05/10',

  4. '1998-03-07',

  5. '12345/23/45678',

  6. '1234/23/56789',

  7. '12345/23/45']

  8. times = times.map((time)=>{

  9. return time.replace(/^(\d{4})[/-](\d{2})[/-](\d{2})$/,(match,p1,p2,p3)=>{

  10. return `${p2}-${p3}-${p1}`

  11. })

  12. });

  13. console.log(times);

  14. //[ '02-03-2006',

  15. 'test/07/sd',

  16. '05-10-2016',

  17. '03-07-1998',

  18. '12345/23/45678',

  19. '1234/23/56789',

  20. '12345/23/45' ]


 

例19

对于str="10000000";如何将其转换成"10.000.000";

var reg = /(?=(\B)(\d{3})+$)/9;
console.log(str.replace(reg,"."));

猜你喜欢

转载自blog.csdn.net/zzddada/article/details/94562063