I have tried for match last character
Here i am passing the One character like A or B, it returns the string ends with passing Character
var name = $(this).find('.name').text();
return name.match( /A$/i );
I have tried for match last character
Here i am passing the One character like A or B, it returns the string ends with passing Character
var name = $(this).find('.name').text();
return name.match( /A$/i );
I am not 100% sure I understand the question correctly buy I suppose you are trying to achieve the following:
'monkey cow lion'.match(/^m.*$/)
Array[ "monkey cow lion" ]
Which will give you the entire string in case it matches.
If you are just looking for the word, I think you can go with:
'monkey cow lion'.match(/^m\w+/)
Array [ "monkey" ]
Do you want to test if the string starts with a particular letter
You can do following:
name.charAt(0) === 'A' ? name : null;
You also can use the /g
instead of /i
.
I guess is is what you're looking for : http://regexr.com/3g74s
So in your JS it will look like this : text.match(/\b[Aa]\w*\b/g);
where [Aa]
is the letters you're looking for.
Or before test match you can lowercase or uppercase the string to test.
If you want to force the string length to be longer than 1 char, you can add {1,}
as the following example : http://regexr.com/3g74v .
The simpliest way is to use Javascript native (ECMAScript 6) startsWith() function. It accepts two parameters:
string.startsWith(searchvalue, start)
so this must work (.toupperCase used to make it case insensitive):
return (name.toUpperCase().startsWith("A".toUpperCase())) ? name : null;
if you need to be case sensitive remove them:
return (name.startsWith("A")) ? name : null;