javascript - 强制填充字符串字段的正则表达式
问题描述
我需要找到一种方法来指导用户填写字符串字段。我需要强制用户以这种方式填充字段:
IP ADRESS (SPACE) PORT (end)
IP ADRESS (SPACE) PORT (end)
例如 :
123.45.70.2 8080
143.23.10.10 433
我需要一个包含 IP 地址和相关端口的列表。
我读了一些关于 RegEx 的东西,但我找不到办法。
我要控制的字段是服务目录项的多行文本变量。
谁能帮我?
谢谢。
解决方案
您可以使用下面的代码使用 javascript 提取给定字符串中的所有 IP 地址:
function findAll(regexPattern, sourceString) {
let output = []
let match
// make sure the pattern has the global flag
let regexPatternWithGlobal = RegExp(regexPattern,"g")
while (match = regexPatternWithGlobal.exec(sourceString)) {
// get rid of the string copy
delete match.input
// store the match data
output.push(match[0].replace(":", " "))
}
return output
}
var str = "123.23.255.123:1233 128.9.88.77:1233";
var ipAddress = findAll("(([0-1]?[0-9]?[0-9]|[2]?[0-5][0-5])\.){3}([0-1]?[0-9][0-9]|[2]?[0-5][0-5])\:[0-9]{4}", str);
RegExp(str, "g")
console.log(ipAddress)
上述代码的输出将是
[ '123.23.255.123 1233', '128.9.88.77 1233' ]
推荐阅读
- sql-server - 如何通过季度和年份以 mm/dd/yyyy 格式返回所有日期
- android - 一个 FCM 令牌可以用于同一用户的多个设备吗?
- java - 构建 AAB 时出错 - Flutter (Android) - 完整性检查失败:java.security.NoSuchAlgorithmException:算法 HmacPBESHA256 不可用
- ios - 更新 contentOffset 时崩溃
- python - 如何在熊猫中仅将数据框的键显示为绘图的 xticks?
- python - 如何从字典列表的字典列表中搜索值并从 Json 格式追加
- python - 拆分列表python的元素
- flutter - 在 Flutter 中使用 hive 登录
- flutter - 在颤动中无法从 dataforseo api 获得 page_metrics 响应。代码在颤动中执行空值
- python-3.x - 导入'app'文件夹时烧瓶应用程序中的ModuleNotFoundError