General
Regex

Regex & Slugify

These Regex and Slugify Functions are used for Validating email and Slugifying String.

Regex Function

regex.ts
function isValidEmail(email: string){
    return /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email) 
}

Regex Usage

index.tsx
const Card = () => {
 
    let isValid: boolean = true;
    if (!isValidEmail(formData.email)) {
        isValid = false;
    } else{
        isValid = true;
    }
    
    return (
        <div>
            <h1>{isValid}</h1>
        </div>
    )
}

Slugify Function

slugify.ts
function spaceToDash(str: string){
    return str.toLowerCase().replace(/ + /g. '-');
};
 
function slugify(str: string){
    return spaceToDash(str).replace(/[^a-zA-Z0-9:/.\- ]+/g, '');
};
 

Slugify Usage

index.tsx
const Info = () => {
 
    const [ input, setInput ] = useState(null);
    return (
        <div>
            <input
                value={input}
                onChange={(e) => {
                    setInput(slugify(e.target.value
                    ))
                }}
            />
        </div>
    )
}