user8209141
user8209141

Reputation:

Node.JS string match only first character

How can I check in regex or with different function that in the text first character contains one of these characters ,._-;<>?! ?

I have this code for ,.;_-

string.match(/[^,.;a-zA-Z0-9_-]|[,;._-]$/igm)

It works, but it check in whole sentences. I want only check the first character. I know, there is IndexOf, but I can't use regex in it.

Upvotes: 2

Views: 772

Answers (1)

Sven
Sven

Reputation: 5265

Do it the other way around. Keep a string of the characters you want to look for, and use the first character of your string to match against those characters.

const characters = ',._-;<>?!'

let myString = '...'
if (characters.includes(myString.charAt(0))) {
  // Do stuff
}

Upvotes: 2

Related Questions