使用负数或数字范围拆分连字符分隔的字符串 - JavaScript?

javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/6 16:12:17

假设我们有以下带有负数或数字范围的连字符分隔字符串−

var firstValue = "John-Smith-90-100-US";
var secondValue = "David-Miller--120-AUS";

要拆分,请使用正则表达式。以下是代码e −

示例

以下是代码 −

var firstValue = "John-Smith-90-100-US";
var secondValue = "David-Miller--120-AUS";
var regularExpression = /-(?=[A-Za-z-]|\d+-\d)/;
var result1 = firstValue.split(regularExpression);
var result2 = secondValue.split(regularExpression);
console.log(result1);
console.log(result2);

要运行上述程序,请使用以下命令 −

node fileName.js.

这里,文件名是 demo241.js。

输出

输出如下 −

PS C:\Users\Amit\javascript-code> node demo241.js
[ 'John', 'Smith', '90-100', 'US' ]
[ 'David', 'Miller', '-120', 'AUS' ]

相关文章


有用资源