I may be asking a stupid Quest, but I'd really like to know if there is there a regular expression to get only the first matched letter from a string?Eg:
var string = "abcdaeee";
var value = 'a';
the value and the string field is dynamic hence they could vary. In above case I'm willing to get only the first matching letter found to the var 'value' (which is 'a') in the string.I currently have this regex:
regex = new RegExp("(" + value + ")", "i");
string.match(regex)
instead can i have something like: o/p: a,a
string.match(someValidRegex)// to o/p: a
but this ones gets all the matching a's from the string. Is there a way in regex to display only the first occurrence of a matching element? Eg o/p: ["a"]
Thanks!
['a', 'a']because you used()group. Change parentheses to[], so it will return just the first letter in the character class. However, you might escape thevaluecharacters