Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regex to check if first and last character are similar?

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

like image 976
Milad Avatar asked Jan 26 '17 10:01

Milad


People also ask

How do I match a character in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does '$' mean in regex?

Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

How do you match a character in JavaScript?

match() is an inbuilt function in JavaScript used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array. Parameters: Here the parameter is “regExp” (i.e. regular expression) which will compare with the given string.


1 Answers

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.

like image 124
Pranav C Balan Avatar answered Sep 20 '22 14:09

Pranav C Balan