Solved

Javascript Regular Expression for removing a "-" in first position of a word?

Posted on 2008-10-30
5
451 Views
Last Modified: 2008-10-31
I need the Javascript Regular Expression for removing a "-" in first position of a word?

For example, how would I remove the "-" from "-MyTest".  I only need to remove the dash if it is in the first position.  
0
Comment
Question by:Hojoformo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 22840423

<script type="text/javascript">
  var re = /^-/;
  var sourcestring = "-source string to match with pattern";
  var result = sourcestring.replace(re, "");
  alert("result = " + result);
</script>

Open in new window

0
 
LVL 15

Expert Comment

by:SRigney
ID: 22840856
You don't need a regular expression for this.

if ( sourcestring !=null && sourcestring.length > 1 && sourcestring.startsWith("-") ) {
  sourcestring = sourcestring.substring(1);
}
0
 
LVL 4

Expert Comment

by:MattKenefick
ID: 22842457
StartsWith is a prototype.
sourcestring = (sourcestring.substr(0,1)=='-' ) ? sourcestring.substring(1):sourcestring;

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22842549
yourstring=yourstring.replace(/\W(-)\w+/g,'');
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22852570
Thanks for the question and the points.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question