每个字符串转换为JavaScript中的另一个字符串都有内置函数吗?
例如
A <=> T
G <=> C
我这样的输入字符串,AAATATATTGC
我想全部转换A to T
T to A
C to G
G to C
。我要输出的是TTTATATAACG
。
在Perl中,针对这种情况很容易做到。
my $s = "AAATATATTGC";
$s =~tr/ATGC/TACG/;
我得到了结果。
像perl一样,有没有可能用JavaScript做到这一点呢?
您可以将其String#replace
与回调一起使用。
// An object to use as replacement
var replacement = {
A: 'T',
T: 'A',
C: 'G',
G: 'C'
};
// Match a single upperCase character from given characters
var result = 'AAATATATTGC'.replace(/[ATCG]/g, function(_) {
return replacement[_]; // Use the value of the key as replacement
});
document.body.innerHTML = result;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句