Result of javascript regular expression not understood - javascript

When I eval (in javascript) [I meant, used string.match()]:
<!--:es-->Text number 1<!--:--><!--:en-->text 2<!--:-->
using
/<!--:es-->(.|\n)*?<!--:-->/
I get as match:
Text number 1,1
I mean, it adds a comma and repeats the last character. Does anybody know why this happens?
PS. text could have carriage return, that is why i used (.|\n).
Thanks a lot.

The result of a regular expression match is an array.
The zero-th element of the array is the whole match : "Text number 1"
The first element of the array is the contents of the first group, in this case "1" since the * is outside the parentheses.
When the array is converted to a string, you get the contents with commas in between.

When I eval (in javascript)
Don't. Use RegExp
Eval() evaluates any ECMAScript, you don't want to do this if you don't have 100% control over the input.

Some research has shown me that the . can't match newlines in javascript.
I'd rewrite your regex this way:
/<!--:es-->[\s\S]*?<!--:-->/
This will avoid the problem you saw, as it excludes the capture group.
And ghoppe is right: use RegExp.

Related

Do not understand the work of the regular expression

"She's the one!".match(/['"](.+?)['"]/g);
Why is the result of Null? The idea is to be a coincidence "She'?
It matches text between two quote mark or apostrophe characters. The string only has one in it. (The " that are used to delimit the string literal are not part of the data in the string).
You're trying to match the outer quotes, which is unneeded
You can get your desired answer by using:
"She's the one!".match(/(.+?)['"]/g);

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.

Nice way to do this regex substitution

I'm writing a javascript function which takes a regex and some elements against which it matches the regex against the name attribute.
Let's say i'm passed this regex
/cmw_step_attributes\]\[\d*\]/
and a string that is structured like this
"foo[bar][]chicken[123][cmw_step_attributes][456][name]"
where all the numbers could vary, or be missing. I want to match the regex against the string in order to swap out the 456 for another number (which will vary), eg 789. So, i want to end up with
"foo[bar][]chicken[123][cmw_step_attributes][789][name]"
The regex will match the string, but i can't swap out the whole regex for 789 as that will wipe out the "[cmw_step_attributes][" bit. There must be a clean and simple way to do this but i can't get my head round it. Any ideas?
thanks, max
Capture the first part and put it back into the string.
.replace(/(cmw_step_attributes\]\[)\d*/, '$1789');
// note I removed the closing ] from the end - quantifiers are greedy so all numbers are selected
// alternatively:
.replace(/cmw_step_attributes\]\[\d*\]/, 'cmw_step_attributes][789]')
Either literally rewrite part that must remain the same in replacement string, or place it inside capturing brackets and reference it in replace.
See answer on: Regular Expression to match outer brackets.
Regular expressions are the wrong tool for the job because you are dealing with nested structures, i.e. recursion.
Have you tried:
var str = 'foo[bar][]chicken[123][cmw_step_attributes][456][name]';
str.replace(/cmw_step_attributes\]\[\d*?\]/gi, 'cmw_step_attributes][XXX]');

Help with a regex

I've got the following sequence I'm attempting to detect...#hl=b&xhr=a where b is equal to anything and a is equal to anything.
I've got the following.. but it doesn't appear to be working... (#hl=.+&xhr=) Does anyone know why?
I'm using javascript and values a and b are letters of the alphabet.
(#hl=.+&xhr=.+), you missed the second .+. Depending on your regex engine, you should also see their escaping rules, often the braces or the + have to be escaped. If you just want to match a whole string, the braces are not needed anyway, btw.
You'll need to be more specific to get a better answer:
what programming language are you using RegEx in?
what values can a and b have? Anything implies that newlines are included, which . doesn't match
do you want to get the values of a and b?
Now that that's all been said, lets move onto a regex with some assumptions:
/#h1=(.+)&xhr=(.+)/
This will match a string #h1=a&xhr=b and select the a and b values from the string. It will be greedy, so if there are key-value pairs in the pseudo-URL (I assume it's a url encoded string as a hashtag) they will be matched in b.
#h1=a&xhr=b&foo=bar
the second selection will match b&foo=bar.
The regex also assumes #h1= comes before &xhr=.
Assuming #, & and = are special characters, how about this regular expression:
#h1=([^#&=]+)&xhr=([^#&=]+)
Are you sure your key/value pairs (?) are always in this order without anything in between?

JS+Regexp: Match anything except if it's between [[ ]]

I've got a <textarea> that will be basically a list of names, so I set a function to replace the spaces between the names for a new line.
Now I need to specify that two or more spaces between names are in fact part of the same element.
IE:
John Lucas [[Laurie Vega]] [[Daniel Deer]] Robert
Should turn to
John
Lucas
[[Laurie Vega]]
[[Daniel Deer]]
Robert
So now my regexp $("textarea").val().toString().replace(\ \g, '\n'); is broken as it will add a new line before Vega and Deer.
I need to replace anything that's not in between [ and ]. I just made the opposite and tried to negate it, but it doesn't seem to work:
// Works
$("textarea").val().toString().match(/\[([^\]]*)\]/g));
// Am I using the ! operand wrong?
$("textarea").val().toString().match(/!\[([^\]]*)\]/g));
I'm a little lost. I tried matching and then replacing, but that way I won't be able to recover my original string. So I have to match anything outside double brackets and replace the space.
If there is a chance that your names contain non alphabetic characters ("Jim-bo O'Leary"?), you may prefer match anything that is not a '[' or a space using /[^[ ]/.
You can then join the matched strings to get the new line effect.
$("textarea").val().toString().match(/([^\[ ]+|\[\[[^\]]*\]\])/g).join("\n");
The exclamation point has no particular meaning in a regex.
What you're looking for is either (that means the | operator) a sequence of letters
[A-Za-z]+
or two brackets, followed by some non-closing-brackets, followed by two closing brackets
\[\[[^\]]+\]\]
So
$("textarea").val().toString().match(/[A-Za-z]+|\[\[[^\]]+\]\]/g)

Categories