如何检查字符串是否只包含点和数字

时间:2017-09-08 23:58:02

标签: javascript regex

检查字符串中仅包含3个点和数字的正则表达式是什么,如下例所示:

let string = 
  "2.3.4.5
   2.3.4.1
   2.3.3.3"

其中string可以包含换行符?

4 个答案:

答案 0 :(得分:2)

我认为这可能是你的追求。

我已经抛出了一些无效的措施。

override func viewDidLoad() {
    super.viewDidLoad()

    let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))

    leftSwipe.direction = .left
    rightSwipe.direction = .right

    self.view.addGestureRecognizer(leftSwipe)
    self.view.addGestureRecognizer(rightSwipe)
}

func handleSwipes(sender: UISwipeGestureRecognizer) {
    if (sender.direction == .left ) {
        headerLabel.text = "Zodiac Yearly"
    }

    if (sender.direction == .right ) {
        headerLabel.text = "Characteristics"
    }
}

答案 1 :(得分:0)

这是正则表达式.test()操作有意义的地方。

/^(\d+\.\d+\.\d+\.\d+)+$/gm.test(str);

答案 2 :(得分:0)

以下是您可以在效用方向上采样的一些逻辑:

with product ( file_path ) as (
       select 'c:\del\img.jpg'               from dual union all
       select '.\prog\total\another_img.gif' from dual union all
       select 'image.gif'                    from dual
     )
-- End of SIMULATED inputs (for testing only, not part of the solution).
-- SQL query begins BELOW THIS LINE.
select substr (file_path, instr(file_path, '\', -1)  + 1) as file_name
from   product
;

FILE_NAME
---------------
img.jpg
another_img.gif
image.gif
  1. let str = `2.3.4.5 2.44.2 2.3.4.1 234.622.166.2346 1. 127.0.0.1. 125.75.23.110 24.66.256.1 sie4wg4t7hweio478tedehyb5ryb Thanks\r ok \n\n 12.34.56.-8 2.3.3.3 3.4.2.4.5 poop .34.5.34. ` // beast mode function to check if string is a valid IP between 0-255 const isValidIP = (ip) => { if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip)) return true return false } const results = str .split('\n') .reduce((all, ip) => { const possibleIP = ip.match(/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/gm) if (possibleIP && isValidIP(ip)) { all.valid.push(ip) } else { all.invalid.push(ip) } return all }, { valid: [], invalid: [] }) console.log(results) 拆分以创建可能匹配的数组
  2. 通过累加器\n
  3. 运行每场比赛
  4. 如果IP有效,请将其添加到reduce()
  5. 如果IP是其他任何内容,请将其添加到results.valid
  6. 调查results.invalid对象

答案 3 :(得分:-1)

匹配有效IPv4地址0.0.0.0 - 255.255.255.255

的正则表达式
"\\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\b"

或者,如果每个人都占据一条线:

"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"

设置^$应该在换行符时匹配。