简体   繁体   English

在Javascript中返回字符串的特定部分

[英]Return specific part of string in Javascript

I would like to manipulate this following string: 20_hbeu50272359_402_21 我想操纵以下字符串: 20_hbeu50272359_402_21

so that I am left with only hbeu50272359 is there a way to how could I delete those outer numbers? 所以我只剩下hbeu50272359有没有办法如何删除那些外部数字? Not sure how to go about it. 不确定如何去做。

The strings may also take this format: 字符串也可以采用以下格式:

hbeu50272359_402_21

You can use split 你可以使用split

"20_hbeu50272359_402_21".split('_')[1]

The above splits your string based on the _ char, then use [1] to select the part you want (in the returned array or ["20", "hbeu50272359", "402", "21"] ) 上面根据_ char分割你的字符串,然后使用[1]选择你想要的部分(在返回的数组中或["20", "hbeu50272359", "402", "21"]

This of course would asume that your strings always follow the same format. 这当然会假设您的字符串始终遵循相同的格式。 if not you could use a regEx match with what appears to be a prefix of hbeu . 如果没有,你可以使用regEx匹配看似hbeu的前缀。 Something like /(hbeu[0-9]+)/ But I'd need more info to give a better approach to the split method /(hbeu[0-9]+)/但我需要更多的信息来提供更好的方法来split方法

UPDATE: 更新:

based on your update you could do a regex match like: 根据您的更新,您可以执行正则表达式匹配,如:

var stuffWeWant = "20_hbeu50272359_402_21".match(/(hbeu[0-9]+)/)[0];
// this will work regardless of the string before and after hbeu50272359.

(?!\\d)[^_]+ will work on both strings and it doesn't require the second string to start with hbeu . (?!\\d)[^_]+将对两个字符串都有效,并且不需要第二个字符串以hbeu

https://regex101.com/r/zF0iR5/3 https://regex101.com/r/zF0iR5/3

You could use string.replace function also. 你也可以使用string.replace函数。 ie, replace 即,替换

  • One or more digits plus the following underscore symbol at the start, 一个或多个数字加上开头的下面的下划线符号,

  • OR 要么

  • underscore plus one or more digits followed by another underscore or end of the line anchor 下划线加上一个或多个数字后跟另一个下划线或行锚点的结尾

with an empty string. 用空字符串。

> "20_hbeu50272359_402_21".replace(/_\d+(?=_|$)|^\d+_/g, "")
'hbeu50272359'
> "hbeu50272359_402_21".replace(/_\d+(?=_|$)|^\d+_/g, "")
'hbeu50272359'

通常,与使用正则表达式相比,拆分解决方案的性能更好。

* is greedy , the engine repeats it as many times as it can, so the regex continues to try to match the _ with next characters, resulting with matching *贪婪的 ,引擎会尽可能多地重复它,所以正则表达式继续尝试将_与下一个字符匹配,从而得到匹配

_hbeu50272359_402_21

In order to make it ungreedy, you should add an ? 为了使它不合适,你应该添加一个? :

_(.*?)_

Now it'll match the first _somecharacters_ . 现在它将匹配第一个_somecharacters_

I would use String.replace() for that: 我会使用String.replace()

var s = "20_hbeu50272359_402_21";
var part = s.replace(/.*?_(.*?)_.*/, '$1');

The search pattern matches any char .* until the next occurrence of a _ . 搜索模式匹配任何char .*直到下一次出现_ Using the ungreedy quantifier ? 使用不合理的量词? makes sure it does not match any char until the last _ in the string. 确保它与字符串中的最后一个_之前的任何字符都不匹配。 The it captures the text until the next _ into a capturing group. 它捕获文本直到下一个_进入捕获组。 (.*?) . (.*?) Then the next _ and the remaining string is matched. 然后匹配下一个_和剩余的字符串。

The replace pattern uses $1 to address the contents of the first capturing group and throws away the remaining match. 替换模式使用$1来解决第一个捕获组的内容并丢弃剩余的匹配。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM