本文介绍了将数字和电子邮件地址之间的所有单词替换为小写,用下划线分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

6/9/1985 1234567890 XYZ ABC [email protected] 301 DURGA NIWAS 

想要转换成:

6/9/1985 1234567890 [email protected] 301 DURGA NIWAS 

所以,我想要的是如果数字之间有任何字母数字字符(例如 1234567890 )和电子邮件(例如 [email protected] )我需要用白色替换小写字符而白色-space将替换为下划线。

So, what i want is if there is any alphanumeric chars between number (e.g. 1234567890) and email (e.g. [email protected]) those I need to replace with lowercase chars while white-space will replace with underscore.

推荐答案

您可以使用替换回调:

var t = '6/9/1985 1234567890 XYZ ABC [email protected] 301 DURGA NIWAS';

var r = t.replace (
    /((?:^|\s)\d+\s+)((?:[a-z]\w*\b\s+)*)(?=[^\s@]+@)/gim, 
    function($0, $1, $2) {
        return $1 + $2.toLowerCase().replace(/\s/g, "_");
    }
);

//=> "6/9/1985 1234567890 [email protected] 301 DURGA NIWAS"

这篇关于将数字和电子邮件地址之间的所有单词替换为小写,用下划线分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:12