Kal
Kal

Reputation: 1774

Modify Regex to convert uppercase to lowercase

I have the following regex

/**
 * For replacing illegal characters
 */
export const replacer = (el, filter) => {
  let elem = new d(el)
  let regex =
    'username' ? /[^a-z0-9_.@$#]/i : filter == 'bio' ? /[<>]/i : null

  elem.on('keyup', e => {
    let value = e.currentTarget.value
    elem.setValue(value.replace(regex, ''))
  })
}

For username, I want to modify /[^a-z0-9_.@$#]/i to also automatically convert any uppercase character that the user types to lowercase.

Upvotes: 0

Views: 559

Answers (1)

Chris Lear
Chris Lear

Reputation: 6742

I think this is what you want

/**
 * For replacing illegal characters
 */
export const replacer = (el, filter) => {
  let elem = new d(el)
  let regex =
    filter == 'username' ? /[^a-z0-9_.@$#]/i : filter == 'bio' ? /[<>]/i : null

  elem.on('keyup', e => {
    let value = e.currentTarget.value
    if (filter == 'username') {
      value = value.toLowerCase()
    }
    elem.setValue(value.replace(regex, ''))
  })
}

All I've done is add .toLowerCase() to the code. Which appears to be what you're asking for. There is no change to the regex, so really this should be tagged javascript I guess.

Upvotes: 1

Related Questions