This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
Pardon me if it seems to be trivial but just to understand regexps:
As said here with character (x) :
(x) matches x and remembers
First part "matches" I can understand but the second part "remembers" is a bit tedious for me to understand.
Can someone please help in explaining it in much easier way?
It's called capturing group. Using backreference ($1, $2, ...), you can reference it in the substitution string:
'R2D5'.replace(/(\d)/g, '$1$1')
// => "R22D55"
You can also use backreference (\1, \2, ...) in the pattern:
'ABBCCDEF'.match(/(.)\1/g) // to match consecutive character
// => ["BB", "CC"]
And you will get additional parameters when you use replacement function:
'R2D5'.replace(/(\d)/g, function(fullMatch, capture1) {
return (parseInt(capture1) + 1).toString();
})
// => "R3D6"
In most regex stuff you can specify a "capturing group" and recall them later:
"something".replace(/so(me)/, '$1 ')
Here, the capturing group is (me) - the result will be me thing
Related
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 1 year ago.
I am trying to find a regex which supports a pattern like below:
String starts with //
String has a delimiter after //; (suppose ; is the delimiter)
String has \n after demiliter (//;\n)
Finally String contains any number of digits with that delimiter (//;\n2;3;4;5)
Could you help?
I tried ^//\\D+\\n.*$ but it doesn't work.
Thanks in advance!
Sample: //;\n2;3;4;5
Answer: [/]{2}[;]\\[n](\d[;]){1,999}\d
This will allow further combinations of a decimal followed by ;
\d is added at the end in the case a semicolon is not added judging by your sample
Okay based on your additional comment this could work. It's very messy but it may just get the job done.
var string = "//;\n2;3;4;5";
console.log(
string.replace(/[^0-9,.]+/g," ").trim().split(" ").map(function(x){return parseInt(x, 10);}).reduce(function(a, b){return a + b;}, 0)
);
Console log results in 14
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I am actually new to javascript and I'm trying to understand what went wrong with this code.
I have a function that accepts a abc as a parameter.
This regular expression was given to me by one of my colleges. I don't have any idea what it's doing.
Just wanted to understand what is the return statement here.
(function(abc) {
var match = abc.match(/(\d+).+?(\d+)/);
return +match[2] + 1;
});
I think the match will contain digits in decimal format but not clear about it.
what will this return? Please let me understand this, will be a great help.
(\d+) - one or more digits (0-9)
.+? - one or more periods (.)
(\d+) - one or more digits (0-9)
Debuggex Demo
You can easily create a snippet and debug it. Using provided example:
function getDiskInfo(diskinfo) {
var match = diskinfo.match(/(\d+).+?(\d+)/);
return +match[2] + 1;
}
console.log(getDiskInfo('111.222'));
In this example, as described by #phuzi:
var match = ['111.222', '111', '222'];
After that your return statement cast your element with index = 2 to Number and increments it by one. So using my example the final result will be 223.
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How do I properly use RegExp?
var text = "here come dat boi o shit waddup";
var exmaple = /[a-zA-Z0-9 ]/; // allowes a-zA-Z0-9 and whitespaces but nothing else right?
example.test(test); // would return true right?
text = "%coconut$§=";
example.test(text); // would return false right?
//I know this is very basic - I started learnig all this about week ago
Are JS RegExp's the same as PHP RegExp's?
How do I define banned characters instead of defining allowed characters?
How do I make it so that the var text has to contain 3 (or more) numbers/letters?
How do I include / or ",'$ etc. in my pattern?
No.
Use ^ character (i.e. [^abc] will exclude a, b and c)
Use [A-Za-z]{3} for letters and \d{3} for digits. If you want 3 or more, use \d{3,}
Use escape character (\/, \', \", '\$')
This question already has answers here:
Regex doesn't omit quotes when matching text in quotes
(3 answers)
Closed 7 years ago.
I'm trying to apply a regular expression to a string, in search of any placeholders, but I can't figure out why the result is only the full match, not the capturing group.
//----HTML------------------------//
<p>Let's try plaintext.<br/>
bommdidumpidudidoo</p>
<p id="output"></p>
//----JS------------------------//
var s = $('p').html();
var matches = s.match( /.*(plaintext)/g );
write(matches);
write(matches[0]);
write(matches[1]);
//------- whatever -------//
function write(s) {
$('#output').html( $('#output').html() +'<br/>'+s );
}
// output:
// Let's try plaintext
// Let's try plaintext
// undefined
» fiddle
(I've used my custom function write instead of console.log so that the result would show up in the fiddle)
Why is the third line undefined? I don't understand!
I'm pretty sure the expression is right. I'm 1oo% certain that this is the right capturing group syntax for JavaScript, and that .match() returns an array with the full match first, then all capturing groups. I've even tested it with regex101.com – over there, I do get the capturing group.
It's not the same as this other problem because there, the OR logic was the crux of the problem and here the pipe | doesn't even come up.
Oooh! It's because I'm doing a global search, with the g modifier. I do get the expected result if I remove that modifier. Tss.
I've used that modifier in the first place in order to grab multiple placeholders, but I guess I can still while-loop that stuff …
This question already has answers here:
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 8 years ago.
Regex and variables seem to be a current issue here, yet I didn't find an answer to my problem.
It is quite simple, i am using a noSQL database system for javascript called nedb. pretty neat by the way. GitHub link
Here is the part that matters for us :
// Finding all planets whose name contain the substring 'ar' using a regular expression
db.find({ planet: /ar/ }, function (err, docs) {
// docs contains Mars and Earth
});
as you see, the expression /ar/ means "containing the substring ar", which is classic regex.
What I want is replacing ar by a variable (the result of a user search).
Like this :
var search = ‘ar'; //taken from an HTML form
db.find ({planet : ‘/‘ + search + ‘/‘}, fonction (err,docs) {
}
This unfortunately does not work. Neither does :
var search = ‘/ar/';
db.find ({planet : search}, fonction (err,docs) {
}
Do you have any ideas ? Might seem prettu obvious to you, but i am losing my mind on this issue aha !
thank you guys
Use the RegExp constructor :
db.find({ planet: new RegExp(yourString) }, function (err, docs) {
If your string can be anything, you'd better escape it. See this related answer then.
You need to use the RegExp constructor, but this alone is not enough (. would be "any character", ? would mean the previous character is optional and / would break your regex, as well as all the other regular regex keywords and characters).
You can avoid this by using a regex escaping function (such aspreg_quote from PHPJS) which will escape them:
db.find({ planet: new RegExp(preg_quote(searchString, '/')) }, function (err, docs) {