简体   繁体   English

regex - 忽略字符串开头的空格

[英]regex - ignore whitespace from the beginning of the string

I have to validate a string into the jquery textcomplete.我必须在 jquery textcomplete 中验证一个字符串。 Now my strings are arragned on multiple lines, a single line may have a couple key = value or multiple separated by commas like:现在我的字符串在多行上排列,单行可能有几个 key = value 或多个用逗号分隔,例如:

key1=value1, mkey2 = value2;
  input3 =  value3

The expression I need must match the left operand of the expression我需要的表达式必须匹配表达式的左操作数

What I did:我做了什么:

/(?:,\s*|^\s|^|,)(?:\s*)([^=]\w*)/

My issue is that it instead of just returning我的问题是它不仅仅是返回

"key1" "mkey2" "input3"

it returns它返回

["key1", "key1"], [", mkey2", "mkey2"] , ["   input3", "input3"]

But I'm expecting (actually jQuery.textcomplete is expecting)但我期待(实际上是 jQuery.textcomplete 期待)

["key1"], ["mkey2"] , ["input3"]

JSFIDDLE JSFIDDLE

EDIT: JS Code (previously was only on jfiddle)编辑:JS 代码(以前只在 jfiddle 上)

var items = [
    "key_1", 
    "key_2", 
    "key",
    "halt",
    "keybone",
    "klingon",
    "kabum"
    ];

$('#myTextArea1').textcomplete(
[{
    match: /[^=](\w*)$/,
    index: 1, // default = 2
    search: function (term, callback) {
        term = term.toLowerCase();
        callback($.map(items, function (item) {
            return item.toLowerCase().indexOf(term) >= 0 ? item : null;
        }));
    },
    replace: function (item) {
        return '' + item + ' = ';
    }
}]);

I'm not sure how you're calling it, but try this out:我不确定你是怎么称呼它的,但试试这个:

\s*(\w+)\s*=

This is the RegEx I was looking for:这是我正在寻找的正则表达式:

/(?<=^|,\s|,)(\w+(?=[=]|$))/m

What does it do?它有什么作用?

(?<=^|,\s|,) is a lookbehind that checks if the string is the beginning ^ , is , or , (?<=^|,\s|,)是一个lookbehind,用于检查字符串是否是开头的^ , is 或,

(\w+(?=[=]|$)) could be decomposed in two sub-expressions: (\w+(?=[=]|$))可以分解为两个子表达式:

<> \w+ that matches a word of at least a letter or digit POSIX [:alnum:] is not supported in JS <> \w+匹配至少一个字母或数字的单词 POSIX [:alnum:]在 JS 中不受支持

<> (?=[=]|$) is a lookahead that keeps the expression only if we're at the end of text $ or the next character is not a = <> (?=[=]|$)是一个前瞻,仅当我们位于文本$的末尾或下一个字符不是=时才保留表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM