Regular Expression to find next word after the match - javascript

I have a statement like Data lva_var type Integer value 20. I need to find out the token after type.
My latest try was [type](?:\s\S+), but the match was e integer.
Code snippets would be helpful.

Just try this, type\s(\w+)Here the first group contains the word next to "type"Hope this code helps.

You can use lookbehind for this (?<=\btype\s)(\w+) will do the trick for you. Take a look at this example.

JavaScript doesn't support lookbehinds, but since you tagged nsregularexpression and that does support them, maybe try this: (?<=\btype\s+)\w+

Related

Regex for - 'A,B','C'

I have written this regex -
([\s]*'[A-Za-z0-9_: ]*[\,]*[\s]*[A-Za-z0-9_: ]*\'[\s]*)[\,]*
But this is not handling the input - 'A,B' 'C' - In this the comma is missing, still its a perfect match.
Can anyone please help.
After giving this more thought, I think what you want is something more like this:
^(?<item>\'[a-zA-Z0-9,\s]+\')(\s*,(?&item))*\s*$
You're using an asterisk which will match zero instances. Try using + instead for the characters you want one or more of.
Please provide other examples that you'd expect to match. For this specific case, the following would match, but is very rigid and specific:
\'+[a-zA-Z]+\,\s*[a-zA-Z]+\'+\,\s*\'+[a-zA-Z]+\'+
Edit:
This is more in line with what I think you want:
^(\'[a-zA-Z]+(\,+\s*[a-zA-Z]+)*\'\s*\,*)*$

Regex expression for accepsts only ()+ and numbers

I need a regular expression to use with Javascript .test
like using this examples
+999904321493214032
and
(+999)432143214
and
432143124321
Can some one please help me with this?
The tightest I can think of is:
^(?:\(\+\d+\)|\+)?\d+$
See live demo showing matching your examples and not matching input with the correct characters but that are malformed (eg imbalanced brackets).

Regular Expression javascript repeat with optional sections

Why doesn't this javascript regexp code work? According to the documentation it should:
Here's my String for parsing (it's the rails nested form information):
flex_table[flex_rows_attributes][0][flex_cells_attributes][0][id]
What I want are these sections, which may or may not be part of the string:
flex_rows_attributes, 0, flex_cells_attributes, 0
Here's my regular expression(as I mentioned, the javascript flavour):
flex_table((\[(\w+)\]\[(\d+)\])*)
I only get back the last of the two entries, but I want them both.
Does anyone know, what I am missing here?
^(?!flex_table\b).*$|(\[(\w+)\]\[(\d+)\])
Try this.Grab the captures.See demo.
http://regex101.com/r/hQ9xT1/26

JScript: how to check if a string contains a specific word?

I am trying to find a way to check if a string contains a specific sequence of characters in JScript.
In my case, I am trying to see if the string is "DPObject" followed by a number. Such as "DPObject3" or "DPObject14".
Thank you!
if (/DPObject\d+/.test(string)) {....}
Javascript String has an indexOf method you can use to check if a String contains a particular substring .
If you need to test for patterns , like "DPObject" followed by an integer , probably you need to use Regexes . ( http://www.regular-expressions.info )
It's javascript , or js for short - not JScript .
Then you should use a regular expression. I think this would be something like :
var re = new RegExp("^DPObject([0-9]+)$");
re.test(someString);
This ensures there is at least only one digit after DPObject.
The "^" at the beginning is to ensure the string starts with DPObject. Check references on regexps for this kind of problems :)
edit: added "$" to mark the end of the string, the updated should be more "solid"
There are a couple of ways:
Use Javascripts indexOf method
Use Javascript Regular Expressions
Use JQuery's contains function
Regular expressions are the most powerful and elegant way of doing it. They syntax makes sense after a while (honestly). ;-)
Good luck.

Javascript Regular Expressions Lookbehind Failing

I am hoping that this will have a pretty quick and simple answer. I am using regular-expressions.info to help me get the right regular expression to turn URL-encoded, ISO-8859-1 pound sign ("%A3"), into a URL-encoded UTF-8 pound sign ("%C2%A3").
In other words I just want to swap %A3 with %C2%A3, when the %A3 is not already prefixed with %C2.
So I would have thought the following would work:
Regular Expression: (?!(\%C2))\%A3
Replace With: %C2%A3
But it doesn't and I can't figure out why!
I assume my syntax is just slightly wrong, but I can't figure it out! Any ideas?
FYI - I know that the following will work (and have used this as a workaround in the meantime), but really want to understand why the former doesn't work.
Regular Expression: ([^\%C2])\%A3
Replace With: $1%C2%A3
TIA!
Why not just replace ((%C2)?%A3) with %C2%A3, making the prefix an optional part of the match? It means that you're "replacing" text with itself even when it's already right, but I don't foresee a performance issue.
Unfortunately, the (?!) syntax is negative lookahead. To the best of my knowledge, JavaScript does not support negative lookbehind.
What you could do is go forward with the replacement anyway, and end up with %C2%C2%A3 strings, but these could easily be converted in a second pass to the desired %C2%A3.
You could replace
(^.?.?|(?!%C2)...)%A3
with
$1%C2%A3
I would suggest you use the functional form of Javascript String.replace (see the section "Specifying a function as a parameter"). This lets you put arbitrary logic, including state if necessary, into a regexp-matching session. For your case, I'd use a simpler regexp that matches a superset of what you want, then in the function call you can test whether it meets your exact criteria, and if it doesn't then just return the matched string as is.
The only problem with this approach is that if you have overlapping potential matches, you have the possibility of missing the second match, since there's no way to return a value to tell the replace() method that it isn't really a match after all.

Categories