PHP/Javascript替换功能的构建
我想请您帮助解决我的问题。 我有这个 php 代码,我用它来清除 $pname
中的介词、空格、数字等,使其更适合排序。
$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])));
我正在尝试将此代码转换为 JavaScript,但代码太大了。这是它的一部分:
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;
}
}
php 结果示例:
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
因此,如果您有想法如何用 javascript 编写它,请帮助我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
布拉德·克里斯蒂
提议:Brad Christie
proposition:好吧,根据您提供的示例以及我可以测试的内容,这就是我得出的结果:
DEMO
再说一遍,仅基于我所看到的。像这样使用它:
Well, based on the examples you gave and what I could test, here's what I came up with:
DEMO
Again, only based on what I could see. Use it like this: