java script string.match() doesn't work as expected - javascript

I am trying to match a string with regex as below. Jsfiddle returns the array with matched string as expected. But if I run the below statement in browser console, it returns null. I tried in ie11,Chrome,Mozilla. Can anybody explain why this discrepancy? Am I missing something.
"201458".match(/^20['^\s']{4,}$|^$/)

It should not match. You are looking for 20 followed by four or more of apostrophe, caret or a whitespace character (followed by a string end); 1 is none of those.
['^] is "apostrophe or caret".
[^'] is "not apostrophe".
Caret only has its special function when at start of the character class; apostrophe does not have any special function in a regexp.
If you find that this matches in jsfiddle, please link the said fiddle; I will be very surprised.

Related

Issue with regex in javascript?

I'm using regex in javascript to check if a string contains a tag called 'tag-name'. See below:
someText
<other>more text<other>
</tag-name>
I want to extract "tag-name", only if it occurs following the first instance of '<' in the string. So in the example above, my regex should not find a match as there is a '<' character at the 'other' tag.
In this example below, it should return 'tag-name' as it occurs after the first occurance of '<'.
someText
</tag-name>
<other></other>
Can anyone suggest how I can get this to work, so far I have used this expression:
<*?tag-name
but, it is extracting the tag-name string regardless of where it occurs.
Thanks for your help.
You can use this regex:
/^[^<]*<[^<]*tag-name/
The logic is to match the start of the string, zero or more non-< characters, <, zero or more non-< characters, then your tag name.
Demo — Be sure to see the Unit Tests (link on the lower left) to demonstrate both of your cases.

Javascript RegEx - invalid quantifier

I saw the other posts but none of them help me ...
So, i tried to match url in a string in javascript with regex it works perfectly on regex101 but fails in javascript.
var matches = feed.content.match(
'/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig'
);
And firebug returns me
SyntaxError: invalid quantifier
Please can you help me ?
As pointed out in the comments, you should remove the single quotes enclosing the regex. As well as that, I would propose making a few changes to the expression itself:
((https?|ftp):\/\/([\w.%-]+\/?)([\w.-]+\/?)*(\?[\w.%+=&:-]*)*)
The ? after the smeans that it is optional, so http and https will both match. \w is the word character class, so that covers A-Za-z0-9_ much more concisely. There's no need to escape all the symbols but a useful trick is to put the - at the end of the character class, so that it isn't interpreted as a range between two characters. The {1} isn't necessary as that's the default behaviour.
updated on regex101
You're passing the regex as a string - just get rid of the outer quotes.
var matches = feed.content.match(
/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig
);

What is the usage for \w?

I have a string: string-a
I'm trying to match this string with the following regex: string-\w{1,2}. I'd like to match anything in the form of "string-aa" where 'aa' can be one or two of any letter or digit.
But running 'string-a'.match('string-\w{1,2}'); returns null in the console. Why does that happen? If I run this in a debugging tool like Debuggex, it works perfectly fine.
I'm not sure I understand how \w is used. Any help would be appreciated!
You are matching as a text not regular expression. you should change it to:
'string-a'.match(/string-(\w{1,2})/);
For 'string-a'.match('string-\w{1,2}'); to work in the console you need to escape the \.
So try,
'string-a'.match('string-\\w{1,2}'); and it will work.
The \w is used as you may expect, it matches a word character.

What does this JavaScript Regular Expression /[^\d.-] mean?

We had a developer here who had added following line of code to a web application:
var amount = newValue.replace(/[^\d.-]/g, '');
The particular line deals with amount values that a user may enter into a field.
I know the following about the regular expression:
that it replaces the matches with empty strings (i.e. removes them)
that /g is a flag that means to match all occurrences inside "newValue"
that the brackets [] denote a special group
that ^ means beginning of the line
that d means digits
Unfortunately I do not know enough to determine what kind of strings this should match. I checked with some web-based regex testers if it matches e.g. strings like 98.- and other alternatives with numbers but so far no luck.
My problem is that it seems to make IE very slow so I need to replace it with something else.
Any help on this would be appreciated.
Edit:
Thanks to all who replied. I tried not just Google but sites like myregextester.com, regular-expressions.info, phpliveregex.com, and others. My problem was misunderstanding the meaning of ^ and expecting that this required a numeric string like 44.99.
Inside the group, when the ^ is the first character, it works as a negation of the character matches. In other words, it's saying match any character that are not the ones in the group.
So this will mean "match anything that is not a digit, a period, or a hyphen".
The ^ character is a negation character.
var newValue = " x44x.-x ";
var amount = newValue.replace(/[^\d.-]/g, '');
console.log(amount);
will print
44.-
I suspect the developer maybe just wanted to remove trailing whitespaces? I would rather try to parse the string for numbers and remove anything else.

Two-digit RegEx Patterm Match in JavaScript

The following expression:
targetString = targetString.replace(parenthesizedRegEx, "$3$1$11");
where parenthesizedRegEx is a valid parenthesized regular expression, replaces the matched text with a string that is the concatenation of the third item, the first item, the first item again, and the literal "1". It is as if it is ignoring the "two-digit" parentheses-item index "$11" and treating it as "$1" and the literal "1".
Is there some escaping or other separating that should be used?
This result occurs in FF and IE9.
Thanks for your help. I hope the answer is embarrassingly simple!!
Edit Update:
I did a jfiddle to demonstrate the issue comprehensively. The regexp I am using includes a negative lookahead assertion. It seems that when I include all the open parens for the assertion, it fails. If I include none of the insertion's open parens, it also fails. But if I include all but the assertion's initial opening paren, it works. I know that groups formed with (?:...) are not numbered. But is seems that one has to include all the other open parens within the assertion to get the count right. So you will see in the jfiddle that $11 does not work but that $10 does.
http://jsfiddle.net/pxMFx/1/
Thanks for looking at this.
This works fine for me:
var regex = /^(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w).*$/i;
alert("abcdefghijklmno".replace(regex,"$3$1$11"));
http://jsfiddle.net/J3RAa/
The key is that you need to match the whole string with your regular expression if you are targeting a specific location in it. Try taking the .* out of the above regex and you will see that it breaks the replacement.

Categories