2

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!

3
  • can you show code that actually does something? you've created 3 variables, and done nothing with them in the code above - i.e.e the code you posted does not output anything, let alone what you claim it outputs Commented Aug 30, 2016 at 23:23
  • It shows ['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 the value characters Commented Aug 30, 2016 at 23:46
  • @Jaromanda X: edited code above and here is the fiddle:jsfiddle.net/mbbrzqef/4 Commented Aug 30, 2016 at 23:57

2 Answers 2

1

Apologies this is the solution for the first matched letter not using regex

If you just want to get the first matched letter of a string i think that regex is a bit overkill.

This would work

    function getFirstMatchedLetter(str,letter){
        var letterPosition = str.search(letter);
        return str.charAt(letterPosition);
    }

    getFirstMatchedLetter("hello","l");
Sign up to request clarification or add additional context in comments.

3 Comments

The OP is trying to get the first matching letter, not the first letter.
I just dont want the first letter of the string, the letter should be matching the value provided, eg: value = 'l', it should o/p: ["l"] instead ["l","l"]
@sarah i have rewritten the answer
0

Alternatively, if possible, you could use two regexes. One to extract the string, then the other to parse the extracted string to get your character or whatever.

var str = "blah blah blah something blah blah blah";

var matched = str.match(/something/);
var character_matched = matched[0].match(/e/);

the variable character_matched should now contain your letter. You can alter 'something' in the var matched line and 'e' in the var character_matched line to suit your needs. However, my question is: what are you extracting just the character in a particular word/string for?

Also, if you're matching a regex more than once, check if you have globals on.


Alternatively:

var str = "hello";
var letter = "p";
var exists = str.search(letter);
if (exists != -1)
{
    # character is in the string, do something
}
else
{
    # character not in string, do something else
}

2 Comments

not sure if I understood you correctly..this is what i tried.. jsfiddle.net/mbbrzqef/1 I'm trying to extract just the first letter so that I can highlight matching character on search string...
Forgot that it returns an array. Here it is working: jsfiddle.net/znee7nL7 And the str is the larger sentence (I thought you were looking for a string inside a sentence, then a character inside the string). If it's as simple as looking for just a character, you could use a modification of Eoghan_Mulcahy's answer. I'll write an example in a moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.