简体   繁体   English

寻找子字符串替代javascript

[英]Looking for substring alternative javascript

Basically my problem is I am using the substring method on the variable version for the result then to be used inside a URL using ng-href:基本上我的问题是我在变量版本上使用 substring 方法作为结果,然后在使用 ng-href 的 URL 中使用:

substring(0, 3)
version 9.1.0 = 9.1 (good)
version 9.2.0 = 9.2 (good)
version 9.3.0 = 9.3 (good)
..
version 9.10.0 = 9.1 (breaks here)
version 10.1.0 = 10. (breaks here)

As you can see eventually the substring method stops working, how can I fix this??正如您所看到的, substring 方法最终停止工作,我该如何解决这个问题?

等效的是substring()检查 MDN 文档

Use split and join on the dot, and during the time you manipulate an array, use slice to remove the last item: 在点上使用splitjoin ,在操作数组期间,使用slice删除最后一项:

 const inputs = ['9.1.0', '9.2.0', '9.3.0', '9.10.0', '10.1.0', '22.121.130']; inputs.forEach(input => { const result = input.split('.').slice(0, -1).join('.'); console.log(input, '=>', result); }) 

Simple enough and it will work whatever your version number :) 非常简单,无论您使用什么版本号,它都可以使用:)

Hoping that will help you! 希望对您有所帮助!

/^\\d+\\.\\d+/ will match the first 2 digits with dot between. /^\\d+\\.\\d+/将匹配前两位,中间用点号。

The regex will not have to process the entire input like the split approaches do. regex不必像split方法那样处理整个输入。

It will also catch consecutive . 它还将连续捕获. like 30..40 . 30..40 And spaces. 和空格。

It will even catch alphabetic parts like 10.B 它甚至会捕获像10.B这样的字母部分

This also will extend if you want to start allowing segments such as -alpha , -beta , etc. 如果您要开始允许-alpha-beta等段,这也将扩展。

 const rx = /^\\d+\\.\\d+/ const inputs = ['9.1.0', '9.2.0', '9.3.0', '9.10.0', '10.1.0', , '22.121.130', '10.A', '10..20', '10. 11', '10 .11']; inputs.forEach(input => { const m = rx.exec(input) console.log(input, m ? m[0] : 'not found') }) 

You can get the substring the other way by removing the last two character which will be a dot and a numeric character: 您可以通过删除最后两个字符(即点和数字字符)来获得子字符串:

 function version(val){ console.log(val.substring(0,val.length-2)) } version('9.1.0'); version('9.2.0'); version('9.3.0'); version('9.10.0'); version('10.1.0'); 

But what if there are two numeric character and a dot at the end? 但是,如果末尾有两个数字字符和一个点怎么办? Here is that solution: 这是解决方案:

 function version(val){ var tempVersion = val.split('.'); tempVersion.pop(); console.log(tempVersion.join('.')) } version('9.1.0'); version('9.2.0'); version('9.3.1020'); version('9.10.0'); version('10.1.0123'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript参数列表; 寻找替代方案 - javascript long list of parameter; looking for alternative 寻找 Javascript 替代 Flash 图像幻灯片 - Looking for a Javascript alternative of Flash Image slideshow replaceAll in JavaScript for 循环太慢,正在寻找替代方法 - replaceAll in JavaScript for loop is too slow, looking for an alternative approach Javascript-寻找替代prependTo的方式,使其不会添加多个项目 - Javascript - looking for an alternative to prependTo so that it doesn't add multiple items 寻找电话号码正则表达式模式的 javascript 的替代品 - Looking for alternative to javascript lookbehind for phone number regex pattern 在属性中寻找子字符串 - Looking for a substring in an attribute 寻找一种方法来遍历字典数组以查看特定的 substring 是否在一个值内——Javascript - Looking for a way to iterate through an array of dictionaries to see if a particular substring is inside a value -- Javascript javascript子字符串 - javascript substring javascript:在数组中查找连接的组件的快速方法(寻找加速/替代方法) - javascript: Fast way to find connected components in array (looking for speed up / alternative way) 寻找使用Javascript从Kendo UI时间选择器中减去时间的更好替代方法 - Looking for a better alternative for using Javascript to subtract time from Kendo UI timepickers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM