Get Part of String - javascript

I am not good at Regular expression and couldn't find an easy way for this problem.
i have an expression like:
TR_NN_Expression
Where NN is a number of 2 digits, and Expression can contain '_', so i can't use split for this. I would like to get the Expression. Any help would be greater appreciated.

You can use this regular expression:
TR_[0-9]{2}_(.*)
The part you want will be in the capturing group. Example usage:
> s = 'TR_01_My##34_Expresion'
"TR_01_My##34_Expresion"
> s.match(/TR_[0-9]{2}_(.*)/)[1]
"My##34_Expresion"

I always use and recommend this tool, It makes our life to easier,
Interactive multi-language regular expression generator
Enjoy!

If the prefix is of fixed length and you know that the strings are of the correct format you can just use substring to accomplish this.
"TR_42_some_expression_here".substring(6) // yields "some_expression_here"
If you have a more complicated situation, regular expressions may be appropriate. The exact expression depends on what you wish to capture.

Related

how to use string searching syntax in javascript

Can someone please explain the syntax of searching through strings? For example, I have this piece of code:
var ok = phone.value.search(/^\d{3}-\d{4}$/);
phone is a variable that is supposed to contain a phone number, and I know from context that this is supposed to make sure the inputted number has the format ###-####, but I don't know what the code within the parenthesis means or how it is evaluated. If someone has a link explaining how to use code like that I would especially appreciate it.
That's a regular expression ( regex ),
Regex One has a good guide on how to use them
Your regex says "beginning with 3 digits, then a "-" then 4 digits"
It's a regular expression, a whole world in itself.
http://www.regular-expressions.info/tutorial.html
It is regex object. The ^ matches the beggining of the string, the \d{3} matches 3 digits, the - matches a dash, the \d{4} matches for digits, and finally the $ matches the end of the string.
What you have there is called a "regular expression" and as you say, they are used to ensure input matches a certain pattern. I recommend you go somewhere like http://www.regular-expressions.info/ for further info rather than re-post data here.

Regex To Sort A String Containing Digits

I have a string which contains digits. I need to sort this string using regular expression.
var myString = "85762034834126745305743";
I'm looking for a complete solution which only use regular expression. Just need your thought on this whether it can be achieved or not.
Regular expressions are not suited for this kind of task. Plain old JavaScript is a lot simpler and easier:
"85762034834126745305743".split("").sort().join("") // "00122333344445556677788"

What is wrong with these regexes with backreferences?

I am trying to make a regex that will match a number of x's that is a power of two. I am using JavaScript. I tried this one:
^(x\1?)$
but it doesn't work. Shouldn't the \1 refer to the outer parathesis so it should match xx, and therefore also xxxx, etc.?
I tried a simpler one that I thought would match x and xx:
^((x)|(\2{2}))$
but this only matches x.
What am I doing wrong?
You can't do "recursive backreferences". At least, it is not so easy.
I'm no sure that you need recuresive regular expressions here. May be you could just count number of the characters in the string and check if it is equal to a power of two?
But if you really need recursive regular expressions (I'm almost sure, you don't),
you can check this question:
Recursive matching with regular expressions in Javascript
and this blog
http://blog.stevenlevithan.com/archives/javascript-match-nested

Solving regular expression recursive strings

The Problem
I could match this string
(xx)
using this regex
\([^()]*\)
But it wouldn't match
(x(xx)x)
So, this regex would
\([^()]*\([^()]*\)[^()]*\)
However, this would fail to match
(x(x(xx)x)x)
But again, this new regex would
[^()]*\([^()]*\([^()]*\)[^()]*\)[^()]*
This is where you can notice the replication, the entire regex pattern of the second regex after the first \( and before the last \) is copied and replaces the center most [^()]*. Of course, this last regex wouldn't match
(x(x(x(xx)x)x)x)
But, you could always copy replace the center most [^()]* with [^()]*\([^()]*\)[^()]* like we did for the last regex and it'll capture more (xx) groups. The more you add to the regex the more it can handle, but it will always be limited to how much you add.
So, how do you get around this limitation and capture a group of parenthesis (or any two characters for that matter) that can contain extra groups within it?
Falsely Assumed Solutions
I know you might think to just use
\(.*\)
But this will match all of
(xx)xx)
when it should only match the sub-string (xx).
Even this
\([^)]*\)
will not match pairs of parentheses that have pairs nested like
(xx(xx)xx)
From this, it'll only match up to (xx(xx).
Is it possible?
So is it possible to write a regex that can match groups of parentheses? Or is this something that must be handled by a routine?
Edit
The solution must work in the JavaScript implementation of Regular Expressions
If you want to match only if the round brackets are balanced you cannot do it by regex itself..
a better way would be to
1>match the string using \(.*\)
2>count the number of (,) and check if they are equal..if they are then you have the match
3>if they are not equal use \([^()]*\) to match the required string
Formally speaking, this isn't possible using regular expressions! Regular expressions define regular languages, and regular languages can't have balanced parenthesis.
However, it turns out that this is the sort of thing people need to do all the time, so lots of Regex engines have been extended to include more than formal regular expressions. Therefore, you can do balanced brackets with regular expressions in javascript. This article might help get you started: http://weblogs.asp.net/whaggard/archive/2005/02/20/377025.aspx . It's for .net, but the same applies for the standard javascript regex engine.
Personally though, I think it's best to solve a complex problem like this with your own function rather than leveraging the extended features of a Regex engine.

Alternatives to (?<=exp) in Javascript?

I read some tutorials about regex and I saw a sentence:
(?<=exp): Match any position following a prefix exp
For example, I have some strings:
Share
Care
If I want to find all string include "are", but "are" must follow "Sh": /(?<=Sh)are/i. Now only "Share" is matched, and matched index is 2 (match "are", not "Share" from "Share").
But Javascript don't have this regex. How can I do like that in Javascript?
Thanks!
You can't do it. There are no lookbehind assertions in Javascript's implementation of regular expressions.
Alternatives
In some situations you can instead use a grouping to capture what you actually wanted to match: /Sh(are)/i
If you really need lookbehinds you could use a third-party regular expression library.
Related
JavaScript: Is there a regular expression library that fully supports lookarounds?
The only way (and of course this only works if you don't also have a lookahead assertion in your regex) is to reverse the string and use a lookahead instead of lookbehind:
/era(?=hS)/i
If I well understood I would use this regexp
/(Sh|\b)(are)/gi
where are can be only a single word or a substring preceded by Sh.
You can use non capturing groups
/(?:sh)(are)/
this tells the regex to find are without capturing the sh group. However in this context, as you have a simple pattern to match, this is not necessary and you can find the answer in other solutions and do something like
/sh(are)/
matching then only the first group

Categories

Resources