Using efvss 3452 XT-123454 ghhs444d efvss XT-6336 ghhsd efq435vss XT-554 ghhsd efvss XT-23427 ghhs55d efvss XT-24
as an example string I want to remove all characters except for the the parts containing XT-number
(eg. XT-123454
).
使用efvss 3452 XT-123454 ghhs444d efvss XT-6336 ghhsd efq435vss XT-554 ghhsd efvss XT-23427 ghhs55d efvss XT-24作为示例字符串我想删除除包含XT编号的部分之外的所有字符(例如XT- 123454)。
I have this code(it doesn't return what I'm looking for):
我有这个代码(它不返回我正在寻找的东西):
$(document).ready(function(){
string = "efvss 3452 XT-123454 ghhs444d efvss XT-6336 ghhsd efq435vss XT-554 ghhsd efvss XT-23427 ghhs55d efvss XT-24";
string = string.match(/(XT-)(.*)(\s)/g);
document.write(string);
});
Desired output should be XT-123454, XT-554, XT-23427, XT-24
期望的输出应为XT-123454,XT-554,XT-23427,XT-24
2 个解决方案
#1
2
You can use regular expressions to match all the XT-##
parts of the string.
您可以使用正则表达式来匹配字符串的所有XT - ##部分。
var str = 'efvss XT-123454 ghhsd efvss XT-6336 ghhsd efvss XT-554 ghhsd efvss XT-23427 ghhsd efvss XT-24';
var regex = /\bXT-\d+\b/g;
var matches = str.match(regex);
< ["XT-123454", "XT-6336", "XT-554", "XT-23427", "XT-24"]
To recreate the string with only the parts you want use .join()
:
要仅使用您想要使用的部分重新创建字符串.join():
var finalString = matches.join(' ');
< "XT-123454 XT-6336 XT-554 XT-23427 XT-24"
#2
0
$(document).ready(function(){
string = "efvss XT-123454 ghhsd efvss XT-6336 ghhsd efvss XT-554 ghhsd efvss XT-23427 ghhsd efvss XT-24";
string = string.match(/(XT-)[0-9]{0,11}/g);
document.write(string);
});
you can use the above code for your desired output https://jsfiddle.net/ygpzq4hj/
你可以使用上面的代码来获得你想要的输出https://jsfiddle.net/ygpzq4hj/
#1
2
You can use regular expressions to match all the XT-##
parts of the string.
您可以使用正则表达式来匹配字符串的所有XT - ##部分。
var str = 'efvss XT-123454 ghhsd efvss XT-6336 ghhsd efvss XT-554 ghhsd efvss XT-23427 ghhsd efvss XT-24';
var regex = /\bXT-\d+\b/g;
var matches = str.match(regex);
< ["XT-123454", "XT-6336", "XT-554", "XT-23427", "XT-24"]
To recreate the string with only the parts you want use .join()
:
要仅使用您想要使用的部分重新创建字符串.join():
var finalString = matches.join(' ');
< "XT-123454 XT-6336 XT-554 XT-23427 XT-24"
#2
0
$(document).ready(function(){
string = "efvss XT-123454 ghhsd efvss XT-6336 ghhsd efvss XT-554 ghhsd efvss XT-23427 ghhsd efvss XT-24";
string = string.match(/(XT-)[0-9]{0,11}/g);
document.write(string);
});
you can use the above code for your desired output https://jsfiddle.net/ygpzq4hj/
你可以使用上面的代码来获得你想要的输出https://jsfiddle.net/ygpzq4hj/