13

Is there any simple way to check if first and last character of a string are the same or not, only with regex?

I know you can check with charAt

var firstChar = str.charAt(0);
var lastChar = str.charAt(length-1);
console.log(firstChar===lastChar):

I'm not asking for this : Regular Expression to match first and last character

Rupesh Chaudhari
  • 308
  • 2
  • 3
  • 12
Milad
  • 27,506
  • 11
  • 76
  • 85
  • Why do you want to use regular expressions for something that's so easily done without regex? – David Thomas Jan 26 '17 at 11:11
  • @DavidThomas , this is not the only rule that I'm checking , I'm checking lot's of others and I was wondering to see if I can do all of them in one regex. – Milad Jan 26 '17 at 11:26

3 Answers3

37

You can use regex with capturing group and its backreference to assert both starting and ending characters are same by capturing the first caharacter. To test the regex match use RegExp#test method.

var regex = /^(.).*\1$/;

console.log(
  regex.test('abcdsa')
)
console.log(
  regex.test('abcdsaasaw')
)

Regex explanation here :

  1. ^ asserts position at start of the string
  2. 1st Capturing Group (.)
  3. .* matches any character (except newline) - between zero and unlimited times, as many times as possible, giving back as needed (greedy)
  4. \1 matches the same text as most recently matched by the 1st capturing group
  5. $ asserts position at the end of the string

The . doesn't include newline character, in order include newline update the regex.

var regex = /^([\s\S])[\s\S]*\1$/;

console.log(
  regex.test(`abcd

sa`)
)
console.log(
  regex.test(`ab
c
dsaasaw`)
)

Refer : How to use JavaScript regex over multiple lines?

Regex explanation here :

  1. [.....] - Match a single character present
  2. \s - matches any whitespace character (equal to [\r\n\t\f\v ])
  3. \S - matches any non-whitespace character (equal to [^\r\n\t\f ])

finally [\s\S] is matches any character.

Community
  • 1
  • 1
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • @Milad : the test method check for pattern match – Pranav C Balan Jan 26 '17 at 11:01
  • The pattern to match any character `[\s\S]` is equivalent to `[^]`. – GOTO 0 Jan 26 '17 at 11:04
  • @PranavCBalan , I know that dude , I meant which part is doing the magi c of checking the first and last char and why? – Milad Jan 26 '17 at 11:05
  • 1
    @Milad : where `^` and `$` assert the start and end position(known as start and end anchors).... then the first char is captured using capturing group and check the last char as the same by back-reference (which is captured, `\1`) – Pranav C Balan Jan 26 '17 at 11:07
  • This is not covering the case with one single character. :) – Andreas Apr 22 '22 at 19:15
1

You can try it

const rg = /^([\w\W]+)[\w\W]*\1$/;
console.log(
  rg.test(`abcda`)
)
console.log(
  rg.test(`aebcdae`)
)
console.log(
  rg.test(`aebcdac`)
)
1
    var rg = /^([a|b])([a|b]+)\1$|^[a|b]$/;

    console.log(rg.test('aabbaa'))

    console.log(rg.test('a'))

    console.log(rg.test('b'))

    console.log(rg.test('bab'))

    console.log(rg.test('baba'))

This will make sure that characters are none other than a and b which have the same start and end.

It will also match single characters because they too start and end with same character.

mOdEL
  • 11
  • 2