Home > Enterprise >  Allow dash or hypen javascript regex
Allow dash or hypen javascript regex

Time:08-05

How can I add to allow the dash(-) or hypen(-)

I need to allow the input text starting with - before the name example -Alyssa or anywhere in the input

Examples

-Alyssa

Alyssa-Reyes

Alyssa-

const NAME_PATTERN = /^[A-Za-z\u00f1\u00d1\u2019\u0027] (([' -][A-Za-z\u00f1\u00d1\u2019\u0027 ])?[A-Za-z\u00f1\u00d1\u2019\u0027]*)*[-]?$/;

UPDATE. I just added - after the \u0027 so it looks like this

const NAME_PATTERN = /^[A-Za-z\u00f1\u00d1\u2019\u0027-] (([' -][A-Za-z\u00f1\u00d1\u2019\u0027 ])?[A-Za-z\u00f1\u00d1\u2019\u0027]*)*[-]?$/;

CodePudding user response:

Try and use a regex like this:

const regex = "^[a-zA-Z0-9_-]*$";

I hope this helps! As a quick reference, here you can test out multiple regex patterns: https://regexr.com/

CodePudding user response:

You could do this more easily with a small Javascript function.

This function could split a string like "Alyssa-Reyes", with the split pattern of alphabetic characters, to recover only the hyphens, and filter the returned array by keeping only filled strings.

Then we can return a boolean using a few operators:

  • Check if our split worked well, if so we have a filled array, otherwise we would have an empty array.
  • Check if all characters are only single hyphen, and therefore not double hyphen.

I didn't know if spaces were allowed like "Alyssa- Reyes" or "Alyssa -Reyes", so I made you 2 variants.

Here's the first variant:

const valuesTest = [
    "Alyssa",
    "--Alyssa-",
    "-Alyssa--Reyes-",
    "Alyssa-Reyes",
    "-Alyssa",
    "-Alyssa-",
    "Alyssa-",
    "Alyssa--",
    "Alyssa- Reyes",
    "Alyssa -Reyes",
];

const checkHyphen = (value) => {
    const hyphens = value.split(/[a-zA-Z]/g).filter(value => value !== "");
    return hyphens.length > 0 && hyphens.every(hyphen => hyphen === "-");
};

valuesTest.forEach(value => {
    console.log(checkHyphen(value) ? `${value} = Valid` : `${value} = Invalid`);
});

Here's the second variant:

In this second variant, I simply applied the .trim() method on each matched value and filtered via the .map() method.

const valuesTest = [
    "Alyssa",
    "--Alyssa-",
    "-Alyssa--Reyes-",
    "Alyssa-Reyes",
    "-Alyssa",
    "-Alyssa-",
    "Alyssa-",
    "Alyssa--",
    "Alyssa- Reyes",
    "Alyssa -Reyes",
];

const checkHyphen = (value) => {
    const hyphens = value.split(/[a-zA-Z]/g).filter(value => value !== "").map(value => value.trim());
    return hyphens.length > 0 && hyphens.every(hyphen => hyphen === "-");
};

valuesTest.forEach(value => {
    console.log(checkHyphen(value) ? `${value} --> Valid` : `${value} --> Invalid`);
});

Good luck to you Alyssa !

CodePudding user response:

What about this regex:

/^(?<=[^-])-?[a-zA-Z-] (?<=[^-])-?$/gm

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

  • lookbehind is used at start and end to dis-allow multiple occurrence of -
  • but inside of the content, multiple -- is allowed ( thought this is the requirement too)
  • Related