JavaScript special characters misunderstanding [duplicate] - javascript

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
In this javascript code I have this row:
var regex = /\s+/gi;
Any idea what is the maning of this:
/\s+/gi
Thank you in advance.

Here is a must read for same https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec and test methods of RegExp, and with the match, replace, search, and split methods of String. This chapter describes JavaScript regular expressions.

Here, each contiguous string of space characters is being replaced with the empty string
For more details refer :-
Is there a difference between /\s/g and /\s+/g?

Related

invalid Regex group [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create the following regex using Javascript.
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
However, by doing this it gives me invalid group error in the console.
regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");
I don't understand where the problem comes from exactly. I appreciate the help.
Thank you
EDIT: After some research I found that Javascript does not support lookbehinds.
So the error comes from (?<!\\).
Refer this newly asked question to find an alternative way to do the same job.
How to check for odd numbers of backslashes in a regex using Javascript?
If your expression isn't dynamic, just use a literal:
var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;
The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.

How can I use Regular Expression to match Chinese characters like ,。‘;’ [duplicate]

This question already has answers here:
Regular expression to match non-ASCII characters?
(8 answers)
Closed 5 years ago.
I'm using javascript to convert string to what I want!!
Can I use Regular Expression and use what Regular Expression??
Is there anyone that can help me?
First, you need to get corresponding Unicode code for these Chinese characters. Here is a helpful tool.
character code(base 10) code(hex)
, 65307 0xff1b
。 65292 0xff0c
; 12290 0x3002
Second, use these Unicode code to form regexp.
js:
/[\u3002\uff0c\uff1b]/.test('A string contains Chinese character。')
true

Convert string into Regular Expression in Javascript [duplicate]

This question already has answers here:
Building regexp from JS variables not working
(5 answers)
Closed 7 years ago.
From the backend of my application, I receive a regular expression which should be matched with a postal code in the frontend.
However, every time I convert to string into a regular expression using the RegExp class, I get another regular expression which doesn't match my postal code anymore.
This is the code I'm currently using (copy from my console):
var str = '/^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$/',
exp = new RegExp(str);
// Returns null
'1055AA'.match(exp);
// The code below does work though...
// Returns: ["1055AA", "AA"]
'1055AA'.match(/^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$/);
Can someone help me solve this problem? Thanks!
Your input string must not begin and end with the Regexp markers / - after all, it's a regular string, not a literal regexp. Also, since it's a regular string (and not (yet) a regexp), you need to double the backslashes as usual in a regular string.

same dynamic regex and inline regex not giving same output in javascript [duplicate]

This question already has answers here:
Backslashes - Regular Expression - Javascript
(2 answers)
Closed 7 years ago.
I have been staring at these two flavors of same regex and can't figure out why the outcome is different:
var projectName="SAMPLE_PROJECT",
fileName="1234_SAMPLE_PROJECT",
re1 = new RegExp('^(\d+)_SAMPLE_PROJECT$','gi'),
re2 = /^(\d+)_SAMPLE_PROJECT$/gi,
matches1 = re1.exec(fileName),
matches2 = re2.exec(fileName);
console.log(matches1);//returns null
console.log(matches2);//returns correctly
Here is the jsbin : https://jsbin.com/badoqokumu/edit?html,js,output
Any idea what I must be doing wrong with instantiating RegExp?
Thanks.
In the first case, you have a string literal, which uses \ to introduce escape sequences. \d in a string is just d. If you want \d, you need to type \\d instead.
In the second case, you have a regular expression literal, which does not interpret \ as a string escape sequence.

How can I use regex to replace all $#8211; with – in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace all occurrences in a string
I found this question/answer:
Use JavaScript regex to replace numerical HTML entities with their actual characters
I just need to replace the one entity though. How can I match that specific pattern with a regex?
I don't know much about regex so I've done this:
.replace('–', '–')
But it obviously only replaces the first instance.
Thanks,
Thomas
The replace method only replaces the first occurance when you are using a string. Use a regular expression, so that you can specify the global flag g:
.replace(/–/g, '–')
.replace(/–/g, '–')
the g flag means global so it replaces all instances.

Categories

Resources