开发者

Construction of PHP/Javascript replace function

I would like to ask you for some help with my problem. I have this php code, which I use to clean $pname from prepositions, white spaces, numbers etc. to make it more suitable for sorting.

$prepositions = array(" ", "tert-", "dl-");         //only a part of  all
$patterns = array("'([0-9])'si", "/\b\w\b\s?/", "/[^a-zA-Z0-9\s]/");
$sname = preg_replace($patterns, '', str_replace($prepositions, "", strtolower($pname])));

I am trying convert this code to javascript but code is so massive. Here is part of it:

function createsname ()
{
    if(document.getElementById('pname').value != '') 
    {
    var myString = new String();
    myString = document.getElementById('pname').value.toLowerCase();
    var reg0 = new RegExp(" ");
    var sname0 = myString.replace(reg0, "");
    var reg1 = new RegExp(/\d/g);
    var sname = sname0.replace(reg1, "");
    document.getElementById('sname').value = sname;
    }
}

Examples of php results:

1,2-Bis(3-Methylthiophene2-yl)ethane-1,2-dione
bismethylthiopheneylethanedione
4H-Cyclopenta[2,1-b3,4-b]dithiophene
cyclopentadithiophene
5-Acetyl-2-amino-4-(2-furanyl)-6-methyl-4H-pyran-3-carbonitrile
acetylaminofuranylmethylpyrancarbonitrile
5-Acetyl-2-amino-4-(4-methoxyphenyl)-6-methyl-4H-pyran-3-carbonitrile
acetylaminomethoxyphenylmethylpyrancarbonitrile
5-Acetyl-2-amino-6-methyl-4-phenyl-4H-pyran-3-carbonitrile
acetylaminomethylphenylpyrancarbonitrile
Tri-O-acetyl-D-glucal
acetylglucal
5-Amino-1,3-dimethyltricyclo(3,3,1,13,7) dekane hydrochloride
aminodimethyltricyclodekanehydrochloride
DL-2-Amino-1-propanol
aminopropanol
(S)-(+)-2-Amino-1-propanol
aminopropanol
a-(3-Aminopropyl)-w-(3-aminopropoxy)-poly (oxy-1,2-ethanediyl)
aminopropylaminopropoxypolyoxyethanediyl
N-(3'-Aminopropyl)-2-pyrrolidone
aminopropylpyrrolidone
1,1,1-(5,5,5-benzene-1,3,5-triyl-tri-thioph开发者_运维技巧en-2-yl)-tris-ethanone
benzenetriyltrithiophenyltrisethanone
N-[(1,4-Benzodioxane-2-yl)carboxyl]piperazine
benzodioxaneylcarboxylpiperazine
(3R)-3-{[(Benzyloxy)carbonyl]amino}butanoic acid
benzyloxycarbonylaminobutanoicacid
1,5-Bis-(2-furanyl)-1,4-pentadiene-3-one
bisfuranylpentadieneone
1-[N,N-Bis(2-hydroxyethyl)amino]-2-propanol
bishydroxyethylaminopropanol
Bisphenol A bis(chloroformate)
bisphenolabischloroformate
Bisphenol A diacetate
bisphenoladiacetate
5''-Bromo-2,2'-5',2''-terthiophene-5-carboxaldehyde
bromoterthiophenecarboxaldehyde
tert-Butyldimethylamine
butyldimethylamine
(E)-3-Chloro-2-methyl-3-(2-thienyl)acrolein
chloromethylthienylacrolein
alpha-Cyano-4-hydroxycinnamic acid
cyanohydroxycinnamicacid
Di-tert-butyl dicarbonate
dibutyldicarbonate
trans-1,4-Dichloro-2-butene
dichlorobutene
a,a-Dichloromethyl methyl ether
dichloromethylmethylether
2,2-Dimethyl-1,3-dioxolane-4-methanol p-toluenesulfonate
dimethyldioxolanemethanolptoluenesulfonate
(R)-2,2-Dimethyl-1,3-dioxolane-4-methanol p-toluenesulfonate
dimethyldioxolanemethanolptoluenesulfonate
4a(R),9b(S)-(-)-cis-2,8-Dimetyl-2,3,4,4a,5,9b-hexahydro-1H-pyrido[4,3-b]indol
dimetylhexahydropyridoindol
4,4'-Dipyridyl N,N'-dioxide hydrate
dipyridylndioxidehydrate
Ethyl 1H-pyrazole-3-carboxylate
ethylpyrazolecarboxylate
Methyl 1H-benzo[4,5]furo[3,2-b]pyrrole-2-carboxylate
methylhbenzofuropyrrolecarboxylate
Methyl 4H-furo[3,2-b]pyrrole-5-carboxylate
methylfuropyrrolecarboxylate
4-Oxo-2,2,6,6-tetramethylpiperidinoxy
oxotetramethylpiperidinoxy
3-Oxo-3-(2-thienyl)propanenitrile
oxothienylpropanenitrile
1,4-Pentadien-3-ol
pentadienol
N-[3-Phenylcoumarinyl-(7)]-N'-(g-N,N'-dimethylaminopropyl)-urea
phenylcoumarinyldimethylaminopropylurea
N,N',N'',N'''-Tetraacetylglycouril
tetraacetylglycouril
2,3,4,6-Tetra-O-benzoyl-b-D-glucopyranosyl isothiocyanate
tetrabenzoylglucopyranosylisothiocyanate
DL-1,2,3,4-Tetrachlorobutane
tetrachlorobutane
2,4,6-Tri-tert-butylaniline
tritertbutylaniline
1,3,5-Tri-tert-butylbenzene
tritertbutylbenzene
3-Amino-2,3,4,5-tetrahydro-2-oxo-2H-1-benzazepin
aminotetrahydrooxobenzazepin
Tris(3-sulfonatophenyl)phosphine hydrate, sodium salt
trissulfonatophenylphosphinehydratesodiumsalt
2-(Tritylamino)-a-(methoxyimino)-4-thiazoleacetic acid hydrochloride
tritylaminomethoxyiminothiazoleaceticacidhydrochloride

So if you have ideas how can i write it in javascript please, help me.


var myString = document.getElementById('pname').value.toLowerCase();
var replaces  = [" ", "tert-", "dl-", /([0-9])/si, /\b\w\b\s?/, /[^a-zA-Z0-9\s]/];

for ( var k in replaces ) {
  myString = myString.replace(replaces[k], "");
}

Brad Christie proposition:

for ( var k = 0; k < replaces.length; k++ ) {
  myString = myString.replaces(replace[k], "");
}


Well, based on the examples you gave and what I could test, here's what I came up with:

String.prototype.Lukas = function(){
    return this.toLowerCase().replace(/(\(.*?\)|\[.*?\]|\b\w\b\s?|(?:dl|tert|\d+[a-z]){1,4}-|\W|\d)/ig, '');
};

DEMO

Again, only based on what I could see. Use it like this:

// assign the original string to a variable
var original = '1,2,4-Triazolo-[4,3,a]pyridin-3(2H)-one';

// then get the desired result using "Lukas()"
var result = original.Lukas();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜