0

Thank you everyone for your great help !

Sorry, I have to edit my question.

What if the "-6.7.8" is a random string that starts with "-" and has two "." between random numbers? such as "-609.7892.805667"?

===============

I am new to JavaScript, could someone help me for the following question?

I have a string AB.CD.1.23.3-609.7.8.EF.HI

I would like to break it into two strings: AB.CD.1.2.3.EF.HI (remove -609.7.8 in the middle) and AB.CD.6.7.8.EF.HI (remove 1.23.3- in the middle).

Is there an easy way to do it?

Thank you very much!

2
  • 1
    you are also removing the trailing digits in 1.2 3 .3 and 6 09 .7.8. Is that correct? Commented Jan 23, 2014 at 15:41
  • Either there is a typo or your results are wrong. I get AB.CD.1.23.3.EF.HI and AB.CD.609.7.8.EF.HI Commented Jan 23, 2014 at 15:46

4 Answers 4

1
var s = "AB.CD.1.23.3-609.7.8.EF.HI";
var a = s.replace("-609.7.8","");
var b = s.replace("1.23.3-","");
console.log(a); //AB.CD.1.23.3.EF.HI
console.log(b); //AB.CD.609.7.8.EF.HI 
Sign up to request clarification or add additional context in comments.

Comments

0

You could use str.replace(); var str = "AB.CD.1.2.3-6.7.8.EF.HI"; var str1 = str.replace("-6.7.8",""); // should return "AB.CD.1.2.3.EF.HI" var str2 = str.replace("1.2.3-",""); // should return "AB.CD.6.7.8.EF.HI"

Comments

0

Use split() in String.prototype.split

var myString = "AB.CD.1.23.3-609.7.8.EF.HI";
var splits1 = myString.split("-609.7.8");
console.log(splits1);
var splits2 = myString.split("1.23.3-");
console.log(splits2);

Comments

0

With regular expressions:

s = 'AB.CD.1.23.3-609.7.8.EF.HI'
var re = /([A-Z]+\.[A-Z]+)\.([0-9]+\.[0-9]+.[0-9]+)-([0-9]+\.[0-9]+.[0-9]+)\.([A-Z]+\.[A-Z]+)/
matches = re.exec(s)
a = matches[1] + '.' + matches[2] + '.' + matches[4] // "AB.CD.1.23.3.EF.HI"
b = matches[1] + '.' + matches[3] + '.' + matches[4] // "AB.CD.609.7.8.EF.HI"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.