-1

I have a string that has some numbers in the middle of the string.

For example,

var str = "abcd-123456.com"

I want to remove the numbers like this

abcd.com

I am not trying to replace all numbers.
I have to replace only -*. expression with "".
How do I do this in JavaScript?

4
  • It is possible. You can use a regular expressions. Commented Oct 21, 2016 at 12:02
  • You could have tried to find this FAQ first? Commented Oct 21, 2016 at 12:03
  • Possible duplicate of JS regex: replace all digits in string Commented Oct 21, 2016 at 12:04
  • i do not want to remove all digits. only wants the digits between - and . only Commented Oct 21, 2016 at 12:07

3 Answers 3

2
var str = "abcd-123456.com"    
str = str.replace(/-[0-9]*/g, '')
Sign up to request clarification or add additional context in comments.

Comments

0

Your comments lead me to believe you want

var str = "abcd-123456.com";
var str1 = str.substring(0,str.indexOf("-"))+str.substring(str.indexOf("."))
console.log(str1);

//or with regex
  
// dasah plus 6 digits to nothing
var str2 = str.replace(/-\d{6}/,"")
console.log(str2);

// dash, digits and an dot to dot
var str3 = str.replace(/-\d+\./,".")
console.log(str3);

Comments

0

AS PER YOUR COMMENT

The answer of Shivaji Varma will nearly do the tricks.

var str = "abc5d-123456.c0om"  
str = str.replace(/-[0-9]*./g, "")
console.log(str)

Soooo,

  • Using the slash indicate a regular expression.
  • [0-9] indicate you want to replace all digit between 0 and 9 (included)
  • "*" to remove all digits
  • "-" and "." to delimite

3 Comments

Raising the post at same time, and just to show other solution he may use. I flagged the post, but I think people need to know why they are doing something useless.
what if i wants to remove - and . too?
str = str.replace(/-[0-9]*./g, "") seems to work. answer updated !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.