1

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-thiophen-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.

4
  • Can you provide sample input/output for this to test against? Commented Apr 19, 2011 at 15:57
  • @Brad Christie: I add some examples to original post Commented Apr 19, 2011 at 18:00
  • (EDIT) Actually, I just looked at the post in edit mode, I think I get it now. Commented Apr 19, 2011 at 18:17
  • Posted an answer. Try that on for size. Commented Apr 19, 2011 at 18:40

2 Answers 2

1
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], "");
}
Sign up to request clarification or add additional context in comments.

1 Comment

should probably use for (var k = 0; k < replace.length; k++) mystring = mystring.replace(replace[k],''); for better portability. Some browsers don't like the javascript "foreach".
0

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();

6 Comments

I checked it with all pnames and it worked good. I made some changes in replace function to get more identical results, but i have still some issues which i am not able to repair. Demo Please, look at first incorrect result, i think its made by last code(|[a-z]+\d.*?), but i do not know, how can i repair it.
@Brad Christie: Other two incorrect results are not so important, but i would like to know if it is possible to get same result as with php. Last question, it is possible replace prepossition (tri-something,tert-something) but only if they are individual, not if they exist together (tri-tert-something) like last example in Demo? I appreciate your help. Thank you.
@Lukas: once again, can you give me a decent-sized sampling of the things you run in to and the values they should be, specifically the odd-ball ones you mentioned? Just re-edit your post, place them in a code block using the pattern: original text, new line, desired result, new line and keep repeating for several entries.
@Brad Christie: OK, I added to my post some more examples of names.
@Lukas: Alright, thank you. I'll see what I can come up with throughout the day. ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.