JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 17th, 2005, 03:28 PM
linhardt linhardt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: missouri
Posts: 165 linhardt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 27 m 51 sec
Reputation Power: 10
Need some more explanation / help with manipulating strings.

Say I have the string:
var X = " This is a test, SHP - 3x3x3x3x3x3x"

I want to find out if that character after "SHP - " is numeric or alpha. If it is alpha then I want to strip "SHP - " from the string.

I know the basic of stripping the string and I think that the below example will strip the "SHP - ". But unsure how to figure out whether the character after "SHP - " is numeric or alpha.

var newstr = X.replace(/SHP - /, "");

Any assistance would be appreciated.

Reply With Quote
  #2  
Old March 17th, 2005, 04:56 PM
IroncomNetworks IroncomNetworks is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 50 IroncomNetworks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 33 m 2 sec
Reputation Power: 9
Send a message via MSN to IroncomNetworks
Using regular expressions is the simplest way to find if something is after something else.

I found this good tutorial on how to do regular expressions in javascript athttp://www.webreference.com/


You should have something like the code below, I have not tested the code.

Code:
var X = " This is a test, SHP - 3x3x3x3x3x3x"
var tmp = x.match(/\w.*SHP(\w.*)/);
if(regex.$1 = null) {
  //nothing after SHP
}
else {
 //something after SHP
}


Everything inside ( ) gets assigned to regex.$1 . If you have more then one set of ( ) in x.match each additional one becomes regex.$1, regex.$2, regex.$3 etc in the order they got declared

the \w says look for any word characters. These include letters, numbers, spaces, and punctuation.

Take a look at the URL I provided. It'll give you good information

Reply With Quote
  #3  
Old March 21st, 2005, 10:49 AM
linhardt linhardt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: missouri
Posts: 165 linhardt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 27 m 51 sec
Reputation Power: 10
I went through the tutorial and kind of got how to accomplish this.

Here is my code:
Code:
<html>
  <head>
  </head>
  <body>
<script>
function testthis (){
var X = "This is a test, SHP - 1"
var reg1 = /^.*SHP - [^0-9]$/;

if (reg1.test(X)) { // if syntax is valid
    alert("\"" + X + "\" has letter after \"SHP - \""); // this is optional
    return true;
  }
  alert("\"" + X + "\" has number after \"SHP - \"");
}

</script>
<input type=button value = "click" onclick="testthis();">
  </body>
</html>


But that only works if you have a single character (whether "alpha" or "numeric") after "SHP - ". But if you have more than one character after "SHP - " then it checks all the characters.
Such as "This is a test, SHP - 1a1a".

What would need to be added to stop after checking the first character after "SHP - "????? All I need to know if the first character after "SHP - " is alpha or numeric. I don't care about the rest.

Last edited by linhardt : March 21st, 2005 at 10:51 AM.

Reply With Quote
  #4  
Old March 21st, 2005, 11:58 AM
linhardt linhardt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: missouri
Posts: 165 linhardt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 27 m 51 sec
Reputation Power: 10
I think I figured it out. Take the "$" out of var reg1 = /^.*SHP - [^0-9]$/; so it reads var reg1 = /^.*SHP - [^0-9]/;

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Need some more explanation / help with manipulating strings.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap