Issue with regex in javascript? - 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.

Related

How to find which part/group of regular expression fails

I need to find which part of expression fails.Let say I have a expression ^-?(\\d*)(,\\d{1,3})*(?:[,]|([.]\\d{0,2}))?$ And I want to know if it fails while matching comma (,) or decimal part . How I can find unmatched group in given regular expression
Break it in to smaller chunks and test that each part matches what you expect it to.
Also as #Avinash Raj has mentioned, online regex checkers like regex101 are indespensible.
These tools highlight what has and hasn't been matched in a given set of data. This will show you where the regex is failing.

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

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.

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

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.

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