Counting size of string with regular expression Javascript - javascript

I am having troubles getting the size of the URL i get with the regular expression. I can print the "exp" content but when i try to make an alert of exp.length it fails:
var pattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
var exp = text.match(pattern);
alert(exp.length);
Any idea?
Thanks.

If, what you're trying to do is get the length of the regexp match result, you would do that like this:
var pattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
var exp = text.match(pattern);
if (exp) {
alert(exp[0].length);
}
.match() returns either null (if no match was found) or an array. In that array, exp[0] would be the whole match that was found. Subsequent indexes in the array contain any subexpressions you matched (denoted with parentheses in the match string).

string.match returns an array, not a string, you are probably trying to do something like this:
var pattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
var exp = text.match(pattern);
alert(exp.join('').length); // alert a string, not an array

Related

Regular Expression returns null

I am kinda stumped, the regular expression found here works: https://regex101.com/r/rD5nP9/1
It finds all of the matches, but when I put it in my code, I get null and I am not sure why.
var value = "name == 'Bob'";
var regex = new RegExp('(.+?)\s(.+?)\s(.+)');
var matches = value.match(regex); // returns null
regex.exec(value); // returns null
What I am trying to get is an array that looks like this:
["name", "==", "'Bob'"]
But for some reason the code isn't finding it, but it works on http://regex101.com and I get a list of matching items.
You need to double escape the backslash or otherwise it would treat \s as an escape sequence.
var regex = new RegExp("(.+?)\\s(.+?)\\s(.+)");
or
Use forward slashes as regex delimiters.
var regex = /(.+?)\s(.+?)\s(.+)/;

Strange result or am I wrong?

I have the following string [row element="div"]This is a row[/row] and I like to extract fromt this string the div property and the string This is a row by using JavaScript and Regex.
The way I am trying to perform this action is the following:
var string = '[row element="div"]This is a row[/row]';
var $d = string.match(/\[row element="([^"]+)"\](.*?)\[\/row\]/g);
console.log($d);
and the result is:
[row element="div"]This is a row[/row]
So, the question is, am I doing it wrong, or the code behaves strange ?
If this is wrong, how can I extract that parts needed from the string variable ?
The problem is that /g and .match are not compatible with subpatterns.
Instead, try the other way around:
var regex = /......../g;
var $d = regex.exec(string);
console.log($d);
The result should be an array, containing the entire match at index 0, the first subpattern as 1, and the second at 2.
You can call .exec repeatedly to see if there are any more [row element="foo"]bar[/row] patterns matching. .exec will return null if there are no more matches.
You can use:
var $d = string.match(/\[row element="(?:[^"]+)"\](.*?)\[\/row\]/)[1];
//=> "This is a row"

Extracting a substring with a JavaScript regular expression;

Consider this code:
var myregexp = "\\*(.+)"; // set from another subsystem, that's why I'm not using a literal regexp
var input = "Paypal *Steam Games";
var output = input.match(new RegExp(myregexp, 'gi'), "$1");
The output is ["*Steam Games"], but I would like it to be just ["Steam Games"].
What is wrong?
PS A great resource I found today: http://regex101.com/#javascript
match doesn’t accept a second argument.
Since you have the global flag set (and I assume it’s intentional), you’ll need exec to find all of the first groups:
var m;
while ((m = re.exec(input)) {
alert(m[1]); // Get group 1
}
var str = "Paypal *Steam Games";
var reg = /\w+\s?\*(\w+\s?\w+)/; // or your exp will work too `/\*(.+)/;`
console.log(reg.exec(str)[1]); // result Steam Games
JSFiddle
You'll get Steam Games from your string with help of /\w+\s?\*(\w+\s?\w+)/ exp
In JavaScript there are three main RegExp functions:
exec A RegExp method that executes a search for a match in a
string. It returns an array of information.
match A String method that executes a search for a match in a
string. It returns an array of information or null on a mismatch.
test A RegExp method that tests for a match in a string. It
returns true or false.

Return only text after last underscore in JavaScript string

If I have a string like so:
var str = 'Arthropoda_Arachnida_Zodariidae_Habronestes_hunti';
How can I get just the last part of the string after the last underscore?
And in the case there are no underscores just return the original string.
In this case I want just 'hunti'
var index = str.lastIndexOf("_");
var result = str.substr(index+1);
It's very simple. Split the string by the underscore, and take the last element.
var last = str.split("_").pop();
This will even work when the string does not contain any underscores (it returns the original string, as desired).
You can use a regular expression:
'Arthropoda_Arachnida_Zodariidae_Habronestes_hunti'.match(/[^_]*$/)[0];

assign matched values from jquery regex match to string variable

I am doing it wrong. I know.
I want to assign the matched text that is the result of a regex to a string var.
basically the regex is supposed to pull out anything in between two colons
so blah:xx:blahdeeblah
would result in xx
var matchedString= $(current).match('[^.:]+):(.*?):([^.:]+');
alert(matchedString);
I am looking to get this to put the xx in my matchedString variable.
I checked the jquery docs and they say that match should return an array. (string char array?)
When I run this nothing happens, No errors in the console but I tested the regex and it works outside of js. I am starting to think I am just doing the regex wrong or I am completely not getting how the match function works altogether
I checked the jquery docs and they say that match should return an array.
No such method exists for jQuery. match is a standard javascript method of a string. So using your example, this might be
var str = "blah:xx:blahdeeblah";
var matchedString = str.match(/([^.:]+):(.*?):([^.:]+)/);
alert(matchedString[2]);
// -> "xx"
However, you really don't need a regular expression for this. You can use another string method, split() to divide the string into an array of strings using a separator:
var str = "blah:xx:blahdeeblah";
var matchedString = str.split(":"); // split on the : character
alert(matchedString[1]);
// -> "xx"
String.match
String.split

Categories

Resources