0

javascriptの正規表現を使用して、一致する他の文字を無視しながら、どのように1つの文字を一致させますか?

例1:$と一致させたいが、$$または$$$とは一致させない。例2:$$に一致させたいが、$$$には一致させたくない。

テストされている典型的な文字列は、「$$$$$$アジアイタリア語」です。

ユーザーエクスペリエンスの観点から、ユーザーは、アイテムのリストにあるタグと値が一致するチェックボックスを選択または選択解除します。アイテムを表示するには、すべてのタグが一致(チェック)されている必要があります。

    function filterResults(){

// Make an array of the checked inputs
var aInputs = $('.listings-inputs input:checked').toArray();
// alert(aInputs);
// Turn that array into a new array made from each items value.
var aValues = $.map(aInputs, function(i){
    // alert($(i).val());
    return $(i).val();
});
// alert(aValues);
// Create new variable, set the value to the joined array set to lower case.
// Use this variable as the string to test
var sValues = aValues.join(' ').toLowerCase();
// alert(sValues);

// sValues = sValues.replace(/\$/ig,'\\$');
// alert(sValues);

// this examines each the '.tags' of each item
$('.listings .tags').each(function(){
    var sTags = $(this).text();
    // alert(sTags);
    sSplitTags = sTags.split(' \267 '); // JavaScript uses octal encoding for special characters
    // alert(sSplitTags);
    // sSplitTags = sTags.split(' \u00B7 '); // This also works

    var show = true;

    $.each(sSplitTags, function(i,tag){

        if(tag.charAt(0) == '$'){
            // alert(tag);
            // alert('It begins with a $');
            // You have to escape special characters for the RegEx
            tag = tag.replace(/\$/ig,'\\$');
            // alert(tag);
        }           

        tag = '\\b' + tag + '\\b';

        var re = new RegExp(tag,'i');

        if(!(re.test(sValues))){
            alert(tag);
            show = false;
            alert('no match');
            return false;
        }
        else{
            alert(tag);
            show = true;
            alert('match');
        }
    });

    if(show == false){
        $(this).parent().hide();
    }
    else{
        $(this).parent().show();
    }

});

// call the swizzleRows function in the listings.js
swizzleList();
}

前もって感謝します!

4

3 に答える 3

2

通常、正規表現では、前後に。が付いていない(?<!x)x(?!x)を一致さ せるために使用できます。xx

最新のECMAScript2018+準拠のJSエンジンでは、ルックビハインドベースの正規表現を使用できます。

(?<!\$)\$(?!\$)

JSデモを参照してください(サポートされているブラウザーでのみ実行してください。ブラウザーの数は増え続けています。ここのリストを確認してください)。

const str ="$ $$ $$$ asian italian";
const regex = /(?<!\$)\$(?!\$)/g;
console.log( str.match(regex).length ); // Count the single $ occurrences
console.log( str.replace(regex, '<span>$&</span>') ); // Enclose single $ occurrences with tags
console.log( str.split(regex) ); // Split with single $ occurrences

于 2020-05-26T10:27:09.933 に答える
1
\bx\b

説明:2つの単語境界間でxを照合します(単語境界の詳細については、このチュートリアルを参照してください)。\b文字列の開始または終了を含みます。

私はあなたの質問で区切られているスペースを利用しています。それがない場合は(^x$|^x[^x]|[^x]x[^x]|[^x]x$)、文字列の最初や最後など、さまざまな位置に一致するような、より複雑な式が必要になります。これにより、単一文字の一致に制限されますが、最初のパターンはトークン全体に一致します。

別の方法は、文字列をトークン化して(スペースで分割して)、トークンからオブジェクトを作成することです。このオブジェクトを検索して、特定の文字列がトークンの1つと一致するかどうかを確認できます。これは、正規表現よりもルックアップごとにはるかに高速である必要があります。

于 2012-10-09T20:48:52.763 に答える
0

そんな感じ:

q=re.match(r"""(x{2})($|[^x])""", 'xx')

q.groups() ('xx', '')

q=re.match(r"""(x{2})($|[^x])""", 'xxx')

q is None True
于 2012-09-28T21:38:21.500 に答える