通过正则表达式检查空值

时间:2019-06-11 02:14:08

标签: javascript regex typescript

我有一个以下格式的字符串。

var commandString = 'Campaign: {campaign value} Group: {group value} Sort_By: {sort by value}'

我有以下两个问题

  1. var commandString = 'Campaign: {campaign value} Group: {group value} Sort_By: {}'的结果是什么?如果值为空,如Sort_By

  2. var commandString = 'Group: {group value} Sort_By: {sort by value}'的用尽是什么如果像Campaign那样一个值是空的当第一个值是空时,则需要删除第二个值的空间,例如Group。

我没有发现任何想法,您能帮我吗?

2 个答案:

答案 0 :(得分:2)

您可以使用此正则表达式

(?:^|\s)[a-z_]+?:\s*\{\s*\}
  • (?:^|\s)-字符串或空格字符的开头
  • [a-z_]+?-匹配字母和_一个或多个时间
  • :-匹配:
  • \s*\{\s*\}-匹配{}

var str = 'Campaign: {campaign value} Group: {group value} Sort_By: {sort by value}'


let removeEmpty = (str) =>{
  return str.replace(/(?:^|\s)[a-z_]+?:\s*\{\s*\}/gi,"").trim()
}

console.log(removeEmpty(str))
console.log(removeEmpty("Campaign: {campaign value} Group: {}"))
console.log(removeEmpty("Campaign: {} Group: {}"))

答案 1 :(得分:0)

我们将设计可选组以涵盖可能的或潜在的选项,例如,我们可以从类似于以下内容的表达式开始

(Campaign:(\s+)?{(.+)?})?(\s+)?Group:(\s+)?{(.+)?}(\s+)?Sort_By:(\s+)?{(.+)?}

Demo 1

const regex = /(Campaign:(\s+)?{(.+)?})?(\s+)?Group:(\s+)?{(.+)?}(\s+)?Sort_By:(\s+)?{(.+)?}/gm;
const str = `Campaign: {campaign value} Group: {group value} Sort_By: {sort by value}
Campaign: {campaign value} Group: {group value} Sort_By: {}
Group: {group value} Sort_By: {sort by value}

Campaign: {campaign value}    Group:   {group value}    Sort_By: {sort by value}
Campaign: {campaign value} Group:   {group value}   Sort_By: {}
Group:   {group value} Sort_By:   {sort by value}`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

,我们可以简单地使用?在我们有时希望拥有的那些组的末尾添加更多可选的空格,并且我们的表达式扩展为:

(Campaign(\s+)?:(\s+)?{(.+)?})?(\s+)?Group(\s+)?:(\s+)?{(.+)?}(\s+)?Sort_By(\s+)?:(\s+)?{(.+)?}

Demo 2

const regex = /(Campaign(\s+)?:(\s+)?{(.+)?})?(\s+)?Group(\s+)?:(\s+)?{(.+)?}(\s+)?Sort_By(\s+)?:(\s+)?{(.+)?}/gm;
const str = `Campaign: {campaign value} Group: {group value} Sort_By: {sort by value}
Campaign: {campaign value} Group: {group value} Sort_By: {}
Group: {group value} Sort_By: {sort by value}

Campaign: {campaign value}    Group:   {group value}    Sort_By: {sort by value}
Campaign: {campaign value} Group:   {group value}   Sort_By: {}
Group:   {group value} Sort_By:   {sort by value}

Campaign   :    {campaign value}    Group:   {group value}    Sort_By: {sort by value}
Campaign  : {campaign value} Group:   {group value}   Sort_By    : {}
Group    :   {group value} Sort_By    :   {sort by value}`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

RegEx电路

jex.im可视化正则表达式:

enter image description here