Javascript Regular expression error in get string between two strings - javascript

I have string look like this :
"fdsgsgf.signature=xxxxx(bv)"
And i want to get xxxxx
With : var testRE = html.match(".signature=(.*)/\(");
And when i run it i get exception that it's not valid regex.
Any idea why?

Some issues with your code:
You're missing starting slash / of your regex
Instead of .* you should better use [^(]+
dot needs to be escaped
Modified code:
html.match(/\.signature=([^(]+)/);

You need to double escape the backslash: ".signature=(.*)/\\(". This is a valid regex, but it will match the / char though. If you don't need it, simply remove it ;)

Related

Javascript - Regular Expression with string template followed by 4 digits number?

Good day. I wanna detect the url string in the <a> tag
Link
whether it matchs the pattern : ?post_type=tribe_events&p=#### (#### = 4 digits number)
I'm writing some Jquery code to detect the expression but the console is throwing the error :
Invalid regular expression: /^(?)post_type=tribe_events&p=^(d{4})/:
Invalid group
var str = $(a).attr("href");
var regexEx = /^(?)post_type=tribe_events&p=^(d{4})/;
var ok = regexEx.exec(str);
console.log(ok);
I'm not good at the regex so I'd be aprreciated if there's any help.
There are couple of issues in your regex.
You need to remove ^ from your regex which denotes start of string and in your case your string doesn't actually start from a ? and is in middle of the string.
You need to escape ? as it has special meaning in regex which is zero or one occurrence of a character.
You need to remove second ^ after p= which isn't needed
You need to write \d and not just d for representing a number.
Also you don't need to group ? and \d{4} unless you really need them.
You corrected regex becomes,
\?post_type=tribe_events&p=\d{4}
Demo
If the test is really what you want, I suppose the right syntax would be:
/^\?post_type=tribe_events&p=\d{4}/

How to Regex MultiVariables

im trying to regex this, but it doesnt work:
this is my string:
asdasd2-bgbegebr23-yiyity23-iopip123
So im trying to get: all values between '-', but it doesnt work: im using actually this:
/(-)(.*)(-)/gi
as regex, but doesnt work, thanks everyone :S
That's because the dot includes de dash. You should remove the dash. Try this:
/([^-]+)/gi
I don't understand very well the purpose of your regex. Supposing that you want to desgin a regular expression that iterated over the example string gets succesively asdasd2, bgbegebr23, etc, the regular expression will be something like this:
\-?([^\-]*)\-?
Why??
- : You have to use "-" instead of "-" because the hyphen is a special char for regular expressions, so you have to escape it
? : The hyphen is optional: in the first case (asdasd2), it is not present. So, enforcing it will omit this first case.
() : Grouping to catch all the asdsas2 and that alphanumeric stuff..
[^-] : Everything except hyphen. The approximation of #vks (.*?) I think will work also
- : Again, a hyphen, but escaped. And we cannot forget the "?" at the end because the last case of the example string, which doesn't end in hyphen.
And don't forget that if you are working in javascript you might need to scape the backslash (), resulting in an expression like this:
\\-?[^\\-]*)\\-?

Javascript Regex Problems : Nothing to repeat

replacedStr = replacedStr.replace(/&^*/g, "asdfasdf");
I need replace all with this regular expression:
/&^*/g
But it doesn't work, I can see the error messages Nothing to repeat in Chrome.
What's wrong with this regex?
The "nothing to repeat" error comes from improper escaping of metacharacters. Both ^ and * are consider special characters meaning the beginning of string anchor and * is a repetition operator. To literally match these characters, you need to properly escape them.
/&\^\*/g
If you're looking to replace those characters anywhere, consider using a character class.
/[&^*]/g
^ is a special meta charcater in regex which matches the start of the line boundary. In-order to match a literal ^ symbol, you need to escape ^ symbol in your regex.
I think you're trying to achieve something like in the below.
> 'foo&^*'.replace(/&\^\*/g, "asdfasdf")
'fooasdfasdf'
> 'foo&^^'.replace(/&\^*/g, "asdfasdf")
'fooasdfasdf'

How to replace a substring with open parentheses (

I am a Regex newbie and trying to implement Regex to replace a matching pattern in a string only when it has a ( - open parentheses using Javascript. for example if I have a string
IN(INTERM_LEVEL_IN + (int)X_ID)
I would only like to highlight the first IN( in the string. Not the INTERM_LEVEL_IN (2 ins here) and the int.
What is the Regex to accomplish this?
To match the opening bracket you just need to escape it: IN\(.
For instance, running this in Firebug console:
enter code here"IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN()/, 'test');`
Will result in:
>>> "IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN\()/, 'test');
"testINTERM_LEVEL_IN + (int)X_ID)"
Parenthesis in regular expressions have a special meaning (sub-capture groups), so when you want them to be interpreted literally you have to escape them by with a \ before them. The regular expression IN\( would match the string IN(.
The following should only match IN( at the beginning of a line:
/^IN\(/
The following would match IN( that is not preceded by any alphanumeric character or underscore:
/[a-zA-Z0-9_]IN\(/
And finally, the following would match any instance of IN( no matter what precedes it:
/IN\(/
So, take your pick. If you're interested in learning more about regex, here's a good tutorial: http://www.regular-expressions.info/tutorial.html
You can use just regular old Javascript for regex, a simple IN\( would work for the example you gave (see here), but I suspect your situation is more complicated than that. In which case, you need to define exactly what you are trying to match and what you don't want to match.

Brackets in Regular Expression

I'd like to compare 2 strings with each other, but I got a little problem with the Brackets.
The String I want to seek looks like this:
CAPPL:LOCAL.L_hk[1].vorlauftemp_soll
Quoting those to bracket is seemingly useless.
I tried it with this code
var regex = new RegExp("CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll","gi");
var value = "CAPPL:LOCAL.L_hk[1].vorlauftemp_soll";
regex.test(value);
Somebody who can help me??
It is useless because you're using string. You need to escape the backslashes as well:
var regex = new RegExp("CAPPL:LOCAL.L_hk\\[1\\].vorlauftemp_soll","gi");
Or use a regex literal:
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi
Unknown escape characters are ignored in JavaScript, so "\[" results in the same string as "[".
In value, you have (1) instead of [1]. So if you expect the regular expression to match and it doesn't, it because of that.
Another problem is that you're using "" in your expression. In order to write regular expression in JavaScript, use /.../g instead of "...".
You may also want to escape the dot in your expression. . means "any character that is not a line break". You, on the other hand, wants the dot to be matched literally: \..
You are generating a regular expression (in which [ is a special character that can be escaped with \) using a string (in which \ is a special character).
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi;

Categories

Resources