Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Regular Expressions - Matching two strings, order doesn't matter

1. ## Regular Expressions - Matching two strings, order doesn't matter

When given two strings:

String A:

Code:
`25,97,84,10,341,0,0,310`
String B:

Code:
`97,25,84,0,310,0,10,341`
They both contain the same numbers just in different orders.

How can I use regular expressions or another method to determine if these strings match disregarding order?

2. you could turn them into arrays, sort them then compare them:

Code:
```<script type = "text/javascript">
var strA="25,97,84,10,341,0,0,310"
var strB="97,25,84,0,310,0,10,341"

var outA=strA.split(",").sort().toString()
var outB=strB.split(",").sort().toString()

if (outA==outB){
} else{
}

</script>```

3. ## Users who have thanked xelawho for this post:

ndm250 (03-23-2013)

4. Originally Posted by xelawho
you could turn them into arrays, sort them then compare them:

Code:
```<script type = "text/javascript">
var strA="25,97,84,10,341,0,0,310"
var strB="97,25,84,0,310,0,10,341"

var outA=strA.split(",").sort().toString()
var outB=strB.split(",").sort().toString()

if (outA==outB){
} else{
}

</script>```
This is perfect. Thanks for the quick response.

5. I thought maybe using the recent every method you could do this:
Code:
```<script type="text/javascript">

arrA = [ 3, 2, 1 ], arrB = [ 1, 2, 3 ];

Array.prototype.compareDimension = function( other )
{
var me = this;

return this.every( function(a){ return other.indexOf( a ) != -1; } ) &&
other.every( function(a){ return me.indexOf( a ) != -1; } )
}