473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

get characters before and after "token"?

I am kind of new to javascript, I am not sure if there is direct support
for "tokens" in the sense that they are characters that mark places in a
string.

For example if I had

var mystring = "12345:abcde";

and I wanted to consider the colon ":" in the middle a token, how would I
define a new variable that is everything that comes before it (12345)? How
about for after it (abcde)?

I am sorry if this is a stupid noob question.

Jun 28 '07 #1
9 15983
d d
Alan wrote:
I am kind of new to javascript, I am not sure if there is direct support
for "tokens" in the sense that they are characters that mark places in a
string.

For example if I had

var mystring = "12345:abcde";

and I wanted to consider the colon ":" in the middle a token, how would I
define a new variable that is everything that comes before it (12345)? How
about for after it (abcde)?

I am sorry if this is a stupid noob question.
If you knew for a fact that there was a : in the
string then you could do this:

alert(mystring.substring(0,mystring.indexOf(":"))) ; //12345
alert(mystring.substring(mystring.indexOf(":")+1)) ; //abcde

or you could split it into an array:

var newarray = mystring.split(":");
alert(newarray[0]); //12345
alert(newarray[1]); //abcde

~dd
Jun 28 '07 #2
Hi,

Thanks very much that works a treat. I have a follow up question now.
Sort of the inverse.

If I have this:

var mystring = "For $100 more (optional) we can upgrade you ($150.00)";

How can I extract the number 150.00?

In this case and the others I need to work with the numerical value is
ALWAYS between the two characters '($' and the single ')' and that part
of the string is ALWAYS at the end. ($150.00) or whatever it is is
always the last characters in the string if that helps.

I have been trying various things with substring and indexof and
lastindex of but I can't quite figure it out.

d d <no*****@please.netwrote in
news:f6*************@news.t-online.com:
Alan wrote:
>I am kind of new to javascript, I am not sure if there is direct
support for "tokens" in the sense that they are characters that mark
places in a string.

For example if I had

var mystring = "12345:abcde";

and I wanted to consider the colon ":" in the middle a token, how
would I define a new variable that is everything that comes before it
(12345)? How about for after it (abcde)?

I am sorry if this is a stupid noob question.

If you knew for a fact that there was a : in the
string then you could do this:

alert(mystring.substring(0,mystring.indexOf(":"))) ; //12345
alert(mystring.substring(mystring.indexOf(":")+1)) ; //abcde

or you could split it into an array:

var newarray = mystring.split(":");
alert(newarray[0]); //12345
alert(newarray[1]); //abcde

~dd
Jun 30 '07 #3
d d
Alan wrote:
If I have this:
var mystring = "For $100 more (optional) we can upgrade you ($150.00)";
How can I extract the number 150.00?
In this case and the others I need to work with the numerical value is
ALWAYS between the two characters '($' and the single ')' and that part
of the string is ALWAYS at the end. ($150.00) or whatever it is is
always the last characters in the string if that helps.
I have been trying various things with substring and indexof and
lastindex of but I can't quite figure it out.
This should work:

var mystring="For $100 (optional) blah ($150.00)";
var start=mystring.lastIndexOf("$")+1;
var end=mystring.lastIndexOf(")");
var val=mystring.substring(start,end);
~dd
Jun 30 '07 #4
d d wrote on 30 jun 2007 in comp.lang.javascript:
Alan wrote:
>If I have this:
var mystring = "For $100 more (optional) we can upgrade you ($150.00)";
How can I extract the number 150.00?
In this case and the others I need to work with the numerical value is
ALWAYS between the two characters '($' and the single ')' and that part
of the string is ALWAYS at the end. ($150.00) or whatever it is is
always the last characters in the string if that helps.
I have been trying various things with substring and indexof and
lastindex of but I can't quite figure it out.

This should work:

var mystring="For $100 (optional) blah ($150.00)";
var start=mystring.lastIndexOf("$")+1;
var end=mystring.lastIndexOf(")");
var val=mystring.substring(start,end);
Regex:

var myString = 'For $100 (optional) blah ($150.00)';
var result = myString.replace(/^.*?\(\$(.*)?\)$/,'$1');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 30 '07 #5
d d
Evertjan. wrote:
d d wrote on 30 jun 2007 in comp.lang.javascript:
>var mystring="For $100 (optional) blah ($150.00)";
var start=mystring.lastIndexOf("$")+1;
var end=mystring.lastIndexOf(")");
var val=mystring.substring(start,end);
Regex:
var myString = 'For $100 (optional) blah ($150.00)';
var result = myString.replace(/^.*?\(\$(.*)?\)$/,'$1');
Cool!! I always try to avoid using Regular expressions.
Mostly because (a) they're impossible for humans to
understand, and (2) I'm a human ;-)

If you're an experienced regexp'er then they're cool.
But don't expect anyone else to understand them. Of
course that in itself can be good for job security ;-)

~dd
Jun 30 '07 #6
d d wrote on 30 jun 2007 in comp.lang.javascript:
Evertjan. wrote:
>d d wrote on 30 jun 2007 in comp.lang.javascript:
>>var mystring="For $100 (optional) blah ($150.00)";
var start=mystring.lastIndexOf("$")+1;
var end=mystring.lastIndexOf(")");
var val=mystring.substring(start,end);
Regex:
var myString = 'For $100 (optional) blah ($150.00)';
var result = myString.replace(/^.*?\(\$(.*)?\)$/,'$1');

Cool!! I always try to avoid using Regular expressions.
Mostly because (a) they're impossible for humans to
understand, and (2) I'm a human ;-)

If you're an experienced regexp'er then they're cool.
But don't expect anyone else to understand them. Of
course that in itself can be good for job security ;-)
Do you mean I should ask money for it?

Regex is very powerfull, fits nicely in javascipt
and can be learned in 2 nights.

I would advice you to spend the time.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 30 '07 #7
d d
Evertjan. wrote:
Regex is very powerfull, fits nicely in javascipt
and can be learned in 2 nights.
I would advice you to spend the time.
Oh yeah, I've done some regexp stuff. I made an
Ant+XML script that I use to minimize/obfuscate
some JS files. But in that case the program to
do that job without regexp would have been a
nightmare. I needed a list of the commands to do
it though :-)

~dd
Jun 30 '07 #8
On Jun 30, 8:41 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
d d wrote on 30 jun 2007 in comp.lang.javascript:
Evertjan. wrote:
d d wrote on 30 jun 2007 in comp.lang.javascript:
var mystring="For $100 (optional) blah ($150.00)";
var start=mystring.lastIndexOf("$")+1;
var end=mystring.lastIndexOf(")");
var val=mystring.substring(start,end);
Regex:
var myString = 'For $100 (optional) blah ($150.00)';
var result = myString.replace(/^.*?\(\$(.*)?\)$/,'$1');
Cool!! I always try to avoid using Regular expressions.
Mostly because (a) they're impossible for humans to
understand, and (2) I'm a human ;-)
If you're an experienced regexp'er then they're cool.
But don't expect anyone else to understand them. Of
course that in itself can be good for job security ;-)

Do you mean I should ask money for it?

Regex is very powerfull, fits nicely in javascipt
and can be learned in 2 nights.

I would advice you to spend the time.
That's good advice. However, "2 nights" may not suffice.

var result = myString.replace(/^.*?\(\$(.*)?\)$/,'$1');

will produce an incorrect result, for example, with:

var myString = 'For $100 (optional) ($300.00) blah ($150.00)';

There are a number of things wrong in the construction of the regular
expression you have proposed, but in particular "?" seems to be
misused or misplaced, or both.

Great care is always required in composing regular expressions, and
even those with a high level of competence in the area need to
carefully test that they produce the desired result. They are
extremely useful, but they are in my experience, not at all easy to
master and use effectively.

var m;
if ( m = myString.match( /^.*\(\$(\d+\.\d{2})\)$/ ) ) {
alert( "Matched: " + m[1] );
}

may serve better.

--
../rh


Jun 30 '07 #9
On Jun 30, 1:49 pm, ron.h.h...@gmail.com wrote:
>
var m;
if ( m = myString.match( /^.*\(\$(\d+\.\d{2})\)$/ ) ) {
alert( "Matched: " + m[1] );

}

may serve better.
And since it's now used in a match rather than replace, the regular
expression can be further simplified to:

/\(\$(\d+\.\d{2})\)$/

--
../rh
Jul 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: linkspeed | last post by:
Hi, I tried following element definition in MSXML 4.0 <xsd:element name="Identifier"> <xsd:simpleType> <xsd:restriction base="xsd:token"> <xsd:pattern value=".*"/> </xsd:restriction>...
1
by: Najm Hashmi | last post by:
Hi all , I am trying to create a store procedure and I get the following error: SQL0104N An unexpected token "END-OF-STATEMENT" was found following "END". Expected tokens may include: "JOIN...
3
by: Frodo Baggins | last post by:
Hi All, I have a piece of code (not written by me) that is failing on compile with the error: pasting "xdr_ndmp_connect_open_request" and "," does not give a valid preprocessing token The...
1
by: G Fernandes | last post by:
Hi, can someone tell me what the following words mean as per C/clc: 1) token 2) token sequence 3) scalar variable 4) vector
10
by: Ian Lazarus | last post by:
Hello. How do "unresolved token" link errors occur. How do I fix them? Linking... LINK : error LNK2020: unresolved token (0A000015) ??_7type_info@@6B@ LINK : error LNK2020: unresolved token...
2
by: P | last post by:
Hi all, I'm trying to run the following code taken from http://blogs.ittoolbox.com/database/technology/archives/006045.asp# select substr(tablespace_name,1,30) as "Tablespace Name", case...
4
by: lisa | last post by:
I have an XML file that starts like this: <?xml version="1.0" encoding="ISO-8859-1" xmlns:fn="http://www.w3.org/2005/xpath-functions"?> <Authors> <Author> <ID>2</ID>...
12
by: Dave H. | last post by:
Please redirect me if this message is posted to the wrong group. Given the intention of delivering content to an HTTP user agent (such as Internet Explorer) which is to be immediately opened by...
0
by: kellygreer1 | last post by:
I'm writing an .ashx page that needs to be able to write files to a network share. Example: string fullPath = @"\\someserver\someshare\log.txt"; File.WriteAllText(fullPath,"test"); I seem to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.