简体   繁体   中英

RegExp match arbitrary string + 8 digits

Trying to match urls in the format of arbitrary string + dash + 8 digits:

yellow-purse-65788544
big-yellow-purse-66784500
iphone-smart-case-water-resistant-55006610

I've built this this one, but it doesn't work:

new RegExp(/^[a-z][A-Z]-\d{8}$/).test('big-yellow-purse-66784500'); // false

Can you help me fix my broken RegExp?

The string is not completely arbitrary. It would be lowercase dashed alpha numeric.

You can use the following regex, which rules out a list of false positives that other answers do not account for (detailed in the Regex101):

^(?:[a-z0-9]+-)+\d{8}$

Regex101

Example construction:

 document.body.textContent = /^(?:[a-z0-9]+-)+\\d{8}$/.test('big-yellow-purse-66784500');

尝试这个:

/^([a-zA-Z]*-)+\d{8}$/.test("iphone-smart-case-water-resistant-55006610");

You string has several dashes ( - ) but the regex just have the last one , try this:

/^[a-z-]+-\d{8}$/im

Regex101 Demo

https://regex101.com/r/rT7xT0/1


Regex explanation:

/^[a-z-]+-\d{8}$/im

    ^ assert position at start of a line
    [a-z-]+ match a single character present in the list below
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
        a-z a single character in the range between a and z (case insensitive)
        - the literal character -
    - matches the character - literally
    \d{8} match a digit [0-9]
        Quantifier: {8} Exactly 8 times
    $ assert position at end of a line
    i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
    m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Demo:

 stringOne = "iphone-smart-case-water-resistant-55006610"; stringtwo = "big-yellow-purse-66784500"; stringThree = "iphone-smart-case-water-resistant-55006610222222"; var myregexp = /^[az-]+-\\d{8}$/im; if(myregexp.test(stringOne)){ document.write(stringOne + " - TRUE<br>"); }else{ document.write(stringOne + " - FALSE<br>"); } if(myregexp.test(stringtwo)){ document.write(stringtwo + " - TRUE<br>"); }else{ document.write(stringtwo + " - FALSE<br>"); } if(myregexp.test(stringThree)){ document.write(stringThree + " - TRUE<br>"); }else{ document.write(stringThree + " - FALSE<br>"); }

If the string really can be arbitrary, you can use this:

/^.*?-\d{8}$/i

The .+? is a non-greedy match for any characters, and \\d{8} says to match exactly 8 digits.

Alternatively, you could use:

/^[\w-]+?-\d{8}$/i

This matches any number of "word" or '-' characters, followed by a '-' and 8 digits.

These both also match the case commonly seen with human-readable URLs in which there are multiple '-' characters in sequence, which can happen by converting something like "Dollar $ign money clip" to "dollar--ign-money clip".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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