-1

I have a string like this:

S(08-01-2008) I[18] ф4(579.1900 UAH) E(05-07-2009)R[5](117.1900) V[0](0.0000) Up(22-04-2009)

I need get Up value: 22-04-2009

6
  • 2
    So what is your question? What have you tried? What's wrong at all ? Commented Aug 30, 2012 at 11:35
  • @Teneff the problem is I can do that on C#, but I don't know javascript Commented Aug 30, 2012 at 11:39
  • @Wachburn You don't get the point! Your question is unclear! Commented Aug 30, 2012 at 11:40
  • @Wachburn : I don't see how he could be more clear. Commented Aug 30, 2012 at 11:42
  • Show us what (pseudo) C# code would solve your problem - we don't know about your string format and can only guess Commented Aug 30, 2012 at 11:48

3 Answers 3

2

Try

'S(08-01-2008) I[18] ф4(579.1900 UAH) E(05-07-2009) R5 V0 Up(22-04-2009)'
 .match(/Up\(.+\)/g);

It returns an array of matched values, here ["Up(22-04-2009)"]

Value only could be:

'S(08-01-2008) I[18] ф4(579.1900 UAH) E(05-07-2009) R5 V0 Up(22-04-2009)'
 .match(/Up\(.+\)/g)[0].split(/\(|\)/)[1];

Or

'S(08-01-2008) I[18] ф4(579.1900 UAH) E(05-07-2009) R5 V0 Up(22-04-2009)'
 .match(/Up\(.+\)/g)[0].replace(/Up\(|\)/g,'');

If you're not sure 'Up(...)' exists in the string:

( 'S(08-01-2008) I[18] ф4(579.1900 UAH) E(05-07-2009) R5 V0 Up(22-04-2009)'
   .match(/Up\(.+\)/g) || ['not found'][0] ) 
 .replace(/Up\(|\)/g,'');
Sign up to request clarification or add additional context in comments.

1 Comment

how can i get value between brackets?
1
extract = yourString.substr( -11,10);

It will extract 10 chars starting by the 11th counting from end.

This assume that the up value is always at the end of your input string.

This one is much faster than regex, or split solutions if you just need one value.

More example at : http://www.bennadel.com/blog/2159-Using-Slice-Substring-And-Substr-In-Javascript.htm

Comments

1

You can easily match a regular expression or get some substring from a certain (or computed) position, but let's try to parse it with a regex and split:

var str = "S(08-01-2008) I[18] ф4(579.1900 UAH) E(05-07-2009)R[5](117.1900) V[0](0.0000) Up(22-04-2009)";

var parts = str.split(/\s*\(\s*(.+?)\s*\)\s*/);
var map = {};
for (var i=0; i+1<parts.length; i+=2)
    map[parts[i]] = parts[i+1];

Now, you can get the "Up" value from the map via

return map["Up"]; // "22-04-2009"

In contrast to the other answers, this will result in undefined if there is no "Up" value in the string instead of throwing an exception. Also, you can get the other values from the map if you need them, for example map["I[18] ф4"] is "I[18] ф4".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.