最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Regex Javascript Match string by first two characters - Stack Overflow

programmeradmin3浏览0评论

I have to match a string by it's first two letters using Regex to check a specific two characters available as the first two letters of a string. Here assume that the first two characters are 'XX'.

And the strings I need to match are

  • ABCDS
  • XXDER
  • DERHJ
  • XXUIO

So I need to filter this list to get strings that only starts with 'XX'

code I tried so far

var filteredArr = [];
var arr = [ "ABCDS ", "XXDER ", "DERHJ ", "XXUIO" ];
var re = new RegExp('^[a-zA-Z]{2}');
jQuery.each( arr, function( i, val ) {

if(re.test(val )){
  filteredArr.push(val);
}
});

What will be the exact Regex pattern to check the string that starts with 'XX'

I have to match a string by it's first two letters using Regex to check a specific two characters available as the first two letters of a string. Here assume that the first two characters are 'XX'.

And the strings I need to match are

  • ABCDS
  • XXDER
  • DERHJ
  • XXUIO

So I need to filter this list to get strings that only starts with 'XX'

code I tried so far

var filteredArr = [];
var arr = [ "ABCDS ", "XXDER ", "DERHJ ", "XXUIO" ];
var re = new RegExp('^[a-zA-Z]{2}');
jQuery.each( arr, function( i, val ) {

if(re.test(val )){
  filteredArr.push(val);
}
});

What will be the exact Regex pattern to check the string that starts with 'XX'

Share Improve this question asked May 17, 2016 at 10:10 tarzanbappatarzanbappa 4,95823 gold badges78 silver badges120 bronze badges 4
  • Do you want to check if first 2 characters are the same? – jcubic Commented May 17, 2016 at 10:12
  • I remend you these tools to avoid headaches : regex101. & txt2re. – Maen Commented May 17, 2016 at 10:13
  • No.. I want to check strings that starts with 'XX' – tarzanbappa Commented May 17, 2016 at 10:13
  • 3 In that case you don't even need regex, just use indexOf – anubhava Commented May 17, 2016 at 10:14
Add a ment  | 

6 Answers 6

Reset to default 7

simply try

var filteredMatches = arr.filter(function(val){
  return val.indexOf("XX") == 0;
});

You can simply use JavaScript .startsWith() method.

var arr = [ "ABCDS ", "XXDER ", "DERHJ ", "XXUIO" ];

var filteredArr = arr.filter(function(val){
  return val.startsWith("XX");
});

console.log(filteredArr);

Try this:

var arr = [ "ABCDS ", "XXDER ", "DERHJ ", "XXUIO" ];
// match if two letters at the beginning are the same
var re = new RegExp('^([a-zA-Z])\\1');
var filteredArr = arr.filter(function(val) {
  return re.test(val);
});
document.body.innerHTML = JSON.stringify(filteredArr);

 var filteredArr = [];
 var arr = [ "ABCDS ", "XXDER ", "DERHJ ", "XXUIO" ];
 var re = new RegExp('^XX');
 jQuery.each( arr, function( i, val ) {

 if(re.test(val )){
 filteredArr.push(val);
 }
});
  • ^ means match at the beginning of the line

Use filter with a less-plex regex:

var filtered = arr.filter(function (el) {
  return /^X{2}/.test(el);
});

DEMO

I suggest string match :

var arr = [ "ABCDS ", "XXDER ", "DERHJ ", "XXUIO" ];

var r = arr.filter(x => x.match(/^XX/))

console.log(r)

发布评论

评论列表(0)

  1. 暂无评论