Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

使用split("-")分隔一个范围字符串为字符串有负数怎么办呀。

使用split分隔一个范围字符串为字符串有负数怎么办呀。

比如:"1-2"用split("-")分隔是正常的,但是"-1-2"使用split("-")就不正常了

只要有负数就不正常了。

想过使用正则表达式但是写不出??

补充一下可能的情况

  • 开始截止都没有:
    1. ""
    1. "-"
  • 只有起始:
    1. "m-" :开始为正数
    1. "-m-" 开始为负数
  • 只有截止:
    1. "-m" 截止为正数
    1. "--m" 截止为负数
  • 开始截止都有:
    1. "m-n" 开始为正数,截止正数
    1. "-m-n" 开始为负数,截止正数
    1. "m--n" 开始正数,截止负数
    1. "-m--n" 开始负数,截止负数

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
'1--2'.match(/(^-?d)-{1}(-?d)/); //["1--2", "1","-2"]
'-1--2'.match(/(^-?d)-{1}(-?d)/); // ["-1--2", "-1","-2"]
'-1-2'.match(/(^-?d)-{1}(-?d)/); // ["-1-2", "-1","2"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...