?
Solved

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

Posted on 2008-10-30
5
Medium Priority
?
453 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

764 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