Two-digit RegEx Patterm Match in JavaScript - 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.

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.

Regex get last 2 characters pipe not working

I'm creating a regex expression to get the variables passed to a JavaScript constructor.
The input is always going to follow along these lines:
app.use(express.static('public'));
And the regex I plan to use to strip out the unnecessary parts is:
(^app.use\()|(..$)
The first part of the regex gets everything up to the first parenthesis, and the it's supposed to pipe it to another expression which gets the last 2 characters of the string.
My issue is that it seems to be ignoring the second regex. I tried a few other expressions in the second part and they worked, but this one isn't.
What am I doing wrong?
Regex example on Regex101: https://regex101.com/r/jV9eH6/3
UPDATE:
This is not a duplicate of How to replace all occurrences of a string in JavaScript?
My question is about a specific issue with a regex, not about replacing one string with another in JavaScript.
You need to use multiline modifier. Whenever anchors ^, $ are used in your regex then feel free to add multi-line modifier m.
/(^app.use\()|(..$)/gm
DEMO

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.

JS regex replace number

Trying to get my head around some regex using JS .replace to replace an integer with a string.
For example, the string could be:
var string = 'image[testing][hello][0][welcome]';
I want to replace the '0' with another value. I was originally using this:
string.replace( /\[\d\]/g, '[newvalue]');
But when we start replacing double digits or more (12, 200, 3204, you get what I mean), it stops working properly. Not sure how to get it functioning the way I want it too.
Thanks in advance. Greatly appreciated.
You need to specify multiple digits:
string.replace( /\[\d+\]/g, '[newvalue]');
JS Fiddle demo
(Note the demo uses jQuery to iterate through the nodes, but it's merely a convenience, and has no bearing on the regular expression, it just demonstrates its function.)
The reason your original didn't work, I think, was because \d matches only a single digit, whereas the + operator/character specifies the preceding (in this case digit) character one or more times.
Reference:
JavaScript Regular Expressions, at the Mozilla Developer Network.
Use the following:
string.replace( /\[\d+\]/g, '[newvalue]');
That should match all digits in brackets.

Categories