如何反转此字符串:हैंI
?
我尝试过:
"हैंI".split("").reverse()
但是我得到了:["I", "ं", "ै", "ह"]
这意味着हैं
被替换为三个字符。
预期输出应为:Iहैं
答案 0 :(得分:3)
使用https://github.com/orling/grapheme-splitter
然后您可以执行以下操作:
function GraphemeSplitter(){var e=0,r=1,n=2,t=3,i=4,u=5,f=6,o=7,a=8,h=9,c=10,l=11,s=12,d=13,v=14,g=15,x=16,p=17,O=0,y=1,m=2,A=3,C=4;function b(e,r){void 0===r&&(r=0);var n=e.charCodeAt(r);if(55296<=n&&n<=56319&&r<e.length-1){var t=n;return 56320<=(i=e.charCodeAt(r+1))&&i<=57343?1024*(t-55296)+(i-56320)+65536:t}if(56320<=n&&n<=57343&&r>=1){var i=n;return 55296<=(t=e.charCodeAt(r-1))&&t<=56319?1024*(t-55296)+(i-56320)+65536:i}return n}function k(l,b,k){var B=[l].concat(b).concat([k]),G=B[B.length-2],S=k,I=B.lastIndexOf(v);if(I>1&&B.slice(1,I).every(function(e){return e==t})&&-1==[t,d,p].indexOf(l))return m;var j=B.lastIndexOf(i);if(j>0&&B.slice(1,j).every(function(e){return e==i})&&-1==[s,i].indexOf(G))return B.filter(function(e){return e==i}).length%2==1?A:C;if(G==e&&S==r)return O;if(G==n||G==e||G==r)return S==v&&b.every(function(e){return e==t})?m:y;if(S==n||S==e||S==r)return y;if(G==f&&(S==f||S==o||S==h||S==c))return O;if(!(G!=h&&G!=o||S!=o&&S!=a))return O;if((G==c||G==a)&&S==a)return O;if(S==t||S==g)return O;if(S==u)return O;if(G==s)return O;var q=-1!=B.indexOf(t)?B.lastIndexOf(t)-1:B.length-2;return-1!=[d,p].indexOf(B[q])&&B.slice(q+1,-1).every(function(e){return e==t})&&S==v?O:G==g&&-1!=[x,p].indexOf(S)?O:-1!=b.indexOf(i)?m:G==i&&S==i?O:y}function B(O){return 1536<=O&&O<=1541||1757==O||1807==O||2274==O||3406==O||69821==O||70082<=O&&O<=70083||72250==O||72326<=O&&O<=72329||73030==O?s:13==O?e:10==O?r:0<=O&&O<=9||11<=O&&O<=12||14<=O&&O<=31||127<=O&&O<=159||173==O||1564==O||6158==O||8203==O||8206<=O&&O<=8207||8232==O||8233==O||8234<=O&&O<=8238||8288<=O&&O<=8292||8293==O||8294<=O&&O<=8303||55296<=O&&O<=57343||65279==O||65520<=O&&O<=65528||65529<=O&&O<=65531||113824<=O&&O<=113827||119155<=O&&O<=119162||917504==O||917505==O||917506<=O&&O<=917535||917632<=O&&O<=917759||918e3<=O&&O<=921599?n:768<=O&&O<=879||1155<=O&&O<=1159||1160<=O&&O<=1161||1425<=O&&O<=1469||1471==O||1473<=O&&O<=1474||1476<=O&&O<=1477||1479==O||1552<=O&&O<=1562||1611<=O&&O<=1631||1648==O||1750<=O&&O<=1756||1759<=O&&O<=1764||1767<=O&&O<=1768||1770<=O&&O<=1773||1809==O||1840<=O&&O<=1866||1958<=O&&O<=1968||2027<=O&&O<=2035||2070<=O&&O<=2073||2075<=O&&O<=2083||2085<=O&&O<=2087||2089<=O&&O<=2093||2137<=O&&O<=2139||2260<=O&&O<=2273||2275<=O&&O<=2306||2362==O||2364==O||2369<=O&&O<=2376||2381==O||2385<=O&&O<=2391||2402<=O&&O<=2403||2433==O||2492==O||2494==O||2497<=O&&O<=2500||2509==O||2519==O||2530<=O&&O<=2531||2561<=O&&O<=2562||2620==O||2625<=O&&O<=2626||2631<=O&&O<=2632||2635<=O&&O<=2637||2641==O||2672<=O&&O<=2673||2677==O||2689<=O&&O<=2690||2748==O||2753<=O&&O<=2757||2759<=O&&O<=2760||2765==O||2786<=O&&O<=2787||2810<=O&&O<=2815||2817==O||2876==O||2878==O||2879==O||2881<=O&&O<=2884||2893==O||2902==O||2903==O||2914<=O&&O<=2915||2946==O||3006==O||3008==O||3021==O||3031==O||3072==O||3134<=O&&O<=3136||3142<=O&&O<=3144||3146<=O&&O<=3149||3157<=O&&O<=3158||3170<=O&&O<=3171||3201==O||3260==O||3263==O||3266==O||3270==O||3276<=O&&O<=3277||3285<=O&&O<=3286||3298<=O&&O<=3299||3328<=O&&O<=3329||3387<=O&&O<=3388||3390==O||3393<=O&&O<=3396||3405==O||3415==O||3426<=O&&O<=3427||3530==O||3535==O||3538<=O&&O<=3540||3542==O||3551==O||3633==O||3636<=O&&O<=3642||3655<=O&&O<=3662||3761==O||3764<=O&&O<=3769||3771<=O&&O<=3772||3784<=O&&O<=3789||3864<=O&&O<=3865||3893==O||3895==O||3897==O||3953<=O&&O<=3966||3968<=O&&O<=3972||3974<=O&&O<=3975||3981<=O&&O<=3991||3993<=O&&O<=4028||4038==O||4141<=O&&O<=4144||4146<=O&&O<=4151||4153<=O&&O<=4154||4157<=O&&O<=4158||4184<=O&&O<=4185||4190<=O&&O<=4192||4209<=O&&O<=4212||4226==O||4229<=O&&O<=4230||4237==O||4253==O||4957<=O&&O<=4959||5906<=O&&O<=5908||5938<=O&&O<=5940||5970<=O&&O<=5971||6002<=O&&O<=6003||6068<=O&&O<=6069||6071<=O&&O<=6077||6086==O||6089<=O&&O<=6099||6109==O||6155<=O&&O<=6157||6277<=O&&O<=6278||6313==O||6432<=O&&O<=6434||6439<=O&&O<=6440||6450==O||6457<=O&&O<=6459||6679<=O&&O<=6680||6683==O||6742==O||6744<=O&&O<=6750||6752==O||6754==O||6757<=O&&O<=6764||6771<=O&&O<=6780||6783==O||6832<=O&&O<=6845||6846==O||6912<=O&&O<=6915||6964==O||6966<=O&&O<=6970||6972==O||6978==O||7019<=O&&O<=7027||7040<=O&&O<=7041||7074<=O&&O<=7077||7080<=O&&O<=7081||7083<=O&&O<=7085||7142==O||7144<=O&&O<=7145||7149==O||7151<=O&&O<=7153||7212<=O&&O<=7219||7222<=O&&O<=7223||7376<=O&&O<=7378||7380<=O&&O<=7392||7394<=O&&O<=7400||7405==O||7412==O||7416<=O&&O<=7417||7616<=O&&O<=7673||7675<=O&&O<=7679||8204==O||8400<=O&&O<=8412||8413<=O&&O<=8416||8417==O||8418<=O&&O<=8420||8421<=O&&O<=8432||11503<=O&&O<=11505||11647==O||11744<=O&&O<=11775||12330<=O&&O<=12333||12334<=O&&O<=12335||12441<=O&&O<=12442||42607==O||42608<=O&&O<=42610||42612<=O&&O<=42621||42654<=O&&O<=42655||42736<=O&&O<=42737||43010==O||43014==O||43019==O||43045<=O&&O<=43046||43204<=O&&O<=43205||43232<=O&&O<=43249||43302<=O&&O<=43309||43335<=O&&O<=43345||43392<=O&&O<=43394||43443==O||43446<=O&&O<=43449||43452==O||43493==O||43561<=O&&O<=43566||43569<=O&&O<=43570||43573<=O&&O<=43574||43587==O||43596==O||43644==O||43696==O||43698<=O&&O<=43700||43703<=O&&O<=43704||43710<=O&&O<=43711||43713==O||43756<=O&&O<=43757||43766==O||44005==O||44008==O||44013==O||64286==O||65024<=O&&O<=65039||65056<=O&&O<=65071||65438<=O&&O<=65439||66045==O||66272==O||66422<=O&&O<=66426||68097<=O&&O<=68099||68101<=O&&O<=68102||68108<=O&&O<=68111||68152<=O&&O<=68154||68159==O||68325<=O&&O<=68326||69633==O||69688<=O&&O<=69702||69759<=O&&O<=69761||69811<=O&&O<=69814||69817<=O&&O<=69818||69888<=O&&O<=69890||69927<=O&&O<=69931||69933<=O&&O<=69940||70003==O||70016<=O&&O<=70017||70070<=O&&O<=70078||70090<=O&&O<=70092||70191<=O&&O<=70193||70196==O||70198<=O&&O<=70199||70206==O||70367==O||70371<=O&&O<=70378||70400<=O&&O<=70401||70460==O||70462==O||70464==O||70487==O||70502<=O&&O<=70508||70512<=O&&O<=70516||70712<=O&&O<=70719||70722<=O&&O<=70724||70726==O||70832==O||70835<=O&&O<=70840||70842==O||70845==O||70847<=O&&O<=70848||70850<=O&&O<=70851||71087==O||71090<=O&&O<=71093||71100<=O&&O<=71101||71103<=O&&O<=71104||71132<=O&&O<=71133||71219<=O&&O<=71226||71229==O||71231<=O&&O<=71232||71339==O||71341==O||71344<=O&&O<=71349||71351==O||71453<=O&&O<=71455||71458<=O&&O<=71461||71463<=O&&O<=71467||72193<=O&&O<=72198||72201<=O&&O<=72202||72243<=O&&O<=72248||72251<=O&&O<=72254||72263==O||72273<=O&&O<=72278||72281<=O&&O<=72283||72330<=O&&O<=72342||72344<=O&&O<=72345||72752<=O&&O<=72758||72760<=O&&O<=72765||72767==O||72850<=O&&O<=72871||72874<=O&&O<=72880||72882<=O&&O<=72883||72885<=O&&O<=72886||73009<=O&&O<=73014||73018==O||73020<=O&&O<=73021||73023<=O&&O<=73029||73031==O||92912<=O&&O<=92916||92976<=O&&O<=92982||94095<=O&&O<=94098||113821<=O&&O<=113822||119141==O||119143<=O&&O<=119145||119150<=O&&O<=119154||119163<=O&&O<=119170||119173<=O&&O<=119179||119210<=O&&O<=119213||119362<=O&&O<=119364||121344<=O&&O<=121398||121403<=O&&O<=121452||121461==O||121476==O||121499<=O&&O<=121503||121505<=O&&O<=121519||122880<=O&&O<=122886||122888<=O&&O<=122904||122907<=O&&O<=122913||122915<=O&&O<=122916||122918<=O&&O<=122922||125136<=O&&O<=125142||125252<=O&&O<=125258||917536<=O&&O<=917631||917760<=O&&O<=917999?t:127462<=O&&O<=127487?i:2307==O||2363==O||2366<=O&&O<=2368||2377<=O&&O<=2380||2382<=O&&O<=2383||2434<=O&&O<=2435||2495<=O&&O<=2496||2503<=O&&O<=2504||2507<=O&&O<=2508||2563==O||2622<=O&&O<=2624||2691==O||2750<=O&&O<=2752||2761==O||2763<=O&&O<=2764||2818<=O&&O<=2819||2880==O||2887<=O&&O<=2888||2891<=O&&O<=2892||3007==O||3009<=O&&O<=3010||3014<=O&&O<=3016||3018<=O&&O<=3020||3073<=O&&O<=3075||3137<=O&&O<=3140||3202<=O&&O<=3203||3262==O||3264<=O&&O<=3265||3267<=O&&O<=3268||3271<=O&&O<=3272||3274<=O&&O<=3275||3330<=O&&O<=3331||3391<=O&&O<=3392||3398<=O&&O<=3400||3402<=O&&O<=3404||3458<=O&&O<=3459||3536<=O&&O<=3537||3544<=O&&O<=3550||3570<=O&&O<=3571||3635==O||3763==O||3902<=O&&O<=3903||3967==O||4145==O||4155<=O&&O<=4156||4182<=O&&O<=4183||4228==O||6070==O||6078<=O&&O<=6085||6087<=O&&O<=6088||6435<=O&&O<=6438||6441<=O&&O<=6443||6448<=O&&O<=6449||6451<=O&&O<=6456||6681<=O&&O<=6682||6741==O||6743==O||6765<=O&&O<=6770||6916==O||6965==O||6971==O||6973<=O&&O<=6977||6979<=O&&O<=6980||7042==O||7073==O||7078<=O&&O<=7079||7082==O||7143==O||7146<=O&&O<=7148||7150==O||7154<=O&&O<=7155||7204<=O&&O<=7211||7220<=O&&O<=7221||7393==O||7410<=O&&O<=7411||7415==O||43043<=O&&O<=43044||43047==O||43136<=O&&O<=43137||43188<=O&&O<=43203||43346<=O&&O<=43347||43395==O||43444<=O&&O<=43445||43450<=O&&O<=43451||43453<=O&&O<=43456||43567<=O&&O<=43568||43571<=O&&O<=43572||43597==O||43755==O||43758<=O&&O<=43759||43765==O||44003<=O&&O<=44004||44006<=O&&O<=44007||44009<=O&&O<=44010||44012==O||69632==O||69634==O||69762==O||69808<=O&&O<=69810||69815<=O&&O<=69816||69932==O||70018==O||70067<=O&&O<=70069||70079<=O&&O<=70080||70188<=O&&O<=70190||70194<=O&&O<=70195||70197==O||70368<=O&&O<=70370||70402<=O&&O<=70403||70463==O||70465<=O&&O<=70468||70471<=O&&O<=70472||70475<=O&&O<=70477||70498<=O&&O<=70499||70709<=O&&O<=70711||70720<=O&&O<=70721||70725==O||70833<=O&&O<=70834||70841==O||70843<=O&&O<=70844||70846==O||70849==O||71088<=O&&O<=71089||71096<=O&&O<=71099||71102==O||71216<=O&&O<=71218||71227<=O&&O<=71228||71230==O||71340==O||71342<=O&&O<=71343||71350==O||71456<=O&&O<=71457||71462==O||72199<=O&&O<=72200||72249==O||72279<=O&&O<=72280||72343==O||72751==O||72766==O||72873==O||72881==O||72884==O||94033<=O&&O<=94078||119142==O||119149==O?u:4352<=O&&O<=4447||43360<=O&&O<=43388?f:4448<=O&&O<=4519||55216<=O&&O<=55238?o:4520<=O&&O<=4607||55243<=O&&O<=55291?a:44032==O||44060==O||44088==O||44116==O||44144==O||44172==O||44200==O||44228==O||44256==O||44284==O||44312==O||44340==O||44368==O||44396==O||44424==O||44452==O||44480==O||44508==O||44536==O||44564==O||44592==O||44620==O||44648==O||44676==O||44704==O||44732==O||44760==O||44788==O||44816==O||44844==O||44872==O||44900==O||44928==O||44956==O||44984==O||45012==O||45040==O||45068==O||45096==O||45124==O||45152==O||45180==O||45208==O||45236==O||45264==O||45292==O||45320==O||45348==O||45376==O||45404==O||45432==O||45460==O||45488==O||45516==O||45544==O||45572==O||45600==O||45628==O||45656==O||45684==O||45712==O||45740==O||45768==O||45796==O||45824==O||45852==O||45880==O||45908==O||45936==O||45964==O||45992==O||46020==O||46048==O||46076==O||46104==O||46132==O||46160==O||46188==O||46216==O||46244==O||46272==O||46300==O||46328==O||46356==O||46384==O||46412==O||46440==O||46468==O||46496==O||46524==O||46552==O||46580==O||46608==O||46636==O||46664==O||46692==O||46720==O||46748==O||46776==O||46804==O||46832==O||46860==O||46888==O||46916==O||46944==O||46972==O||47e3==O||47028==O||47056==O||47084==O||47112==O||47140==O||47168==O||47196==O||47224==O||47252==O||47280==O||47308==O||47336==O||47364==O||47392==O||47420==O||47448==O||47476==O||47504==O||47532==O||47560==O||47588==O||47616==O||47644==O||47672==O||47700==O||47728==O||47756==O||47784==O||47812==O||47840==O||47868==O||47896==O||47924==O||47952==O||47980==O||48008==O||48036==O||48064==O||48092==O||48120==O||48148==O||48176==O||48204==O||48232==O||48260==O||48288==O||48316==O||48344==O||48372==O||48400==O||48428==O||48456==O||48484==O||48512==O||48540==O||48568==O||48596==O||48624==O||48652==O||48680==O||48708==O||48736==O||48764==O||48792==O||48820==O||48848==O||48876==O||48904==O||48932==O||48960==O||48988==O||49016==O||49044==O||49072==O||49100==O||49128==O||49156==O||49184==O||49212==O||49240==O||49268==O||49296==O||49324==O||49352==O||49380==O||49408==O||49436==O||49464==O||49492==O||49520==O||49548==O||49576==O||49604==O||49632==O||49660==O||49688==O||49716==O||49744==O||49772==O||49800==O||49828==O||49856==O||49884==O||49912==O||49940==O||49968==O||49996==O||50024==O||50052==O||50080==O||50108==O||50136==O||50164==O||50192==O||50220==O||50248==O||50276==O||50304==O||50332==O||50360==O||50388==O||50416==O||50444==O||50472==O||50500==O||50528==O||50556==O||50584==O||50612==O||50640==O||50668==O||50696==O||50724==O||50752==O||50780==O||50808==O||50836==O||50864==O||50892==O||50920==O||50948==O||50976==O||51004==O||51032==O||51060==O||51088==O||51116==O||51144==O||51172==O||51200==O||51228==O||51256==O||51284==O||51312==O||51340==O||51368==O||51396==O||51424==O||51452==O||51480==O||51508==O||51536==O||51564==O||51592==O||51620==O||51648==O||51676==O||51704==O||51732==O||51760==O||51788==O||51816==O||51844==O||51872==O||51900==O||51928==O||51956==O||51984==O||52012==O||52040==O||52068==O||52096==O||52124==O||52152==O||52180==O||52208==O||52236==O||52264==O||52292==O||52320==O||52348==O||52376==O||52404==O||52432==O||52460==O||52488==O||52516==O||52544==O||52572==O||52600==O||52628==O||52656==O||52684==O||52712==O||52740==O||52768==O||52796==O||52824==O||52852==O||52880==O||52908==O||52936==O||52964==O||52992==O||53020==O||53048==O||53076==O||53104==O||53132==O||53160==O||53188==O||53216==O||53244==O||53272==O||53300==O||53328==O||53356==O||53384==O||53412==O||53440==O||53468==O||53496==O||53524==O||53552==O||53580==O||53608==O||53636==O||53664==O||53692==O||53720==O||53748==O||53776==O||53804==O||53832==O||53860==O||53888==O||53916==O||53944==O||53972==O||54e3==O||54028==O||54056==O||54084==O||54112==O||54140==O||54168==O||54196==O||54224==O||54252==O||54280==O||54308==O||54336==O||54364==O||54392==O||54420==O||54448==O||54476==O||54504==O||54532==O||54560==O||54588==O||54616==O||54644==O||54672==O||54700==O||54728==O||54756==O||54784==O||54812==O||54840==O||54868==O||54896==O||54924==O||54952==O||54980==O||55008==O||55036==O||55064==O||55092==O||55120==O||55148==O||55176==O?h:44033<=O&&O<=44059||44061<=O&&O<=44087||44089<=O&&O<=44115||44117<=O&&O<=44143||44145<=O&&O<=44171||44173<=O&&O<=44199||44201<=O&&O<=44227||44229<=O&&O<=44255||44257<=O&&O<=44283||44285<=O&&O<=44311||44313<=O&&O<=44339||44341<=O&&O<=44367||44369<=O&&O<=44395||44397<=O&&O<=44423||44425<=O&&O<=44451||44453<=O&&O<=44479||44481<=O&&O<=44507||44509<=O&&O<=44535||44537<=O&&O<=44563||44565<=O&&O<=44591||44593<=O&&O<=44619||44621<=O&&O<=44647||44649<=O&&O<=44675||44677<=O&&O<=44703||44705<=O&&O<=44731||44733<=O&&O<=44759||44761<=O&&O<=44787||44789<=O&&O<=44815||44817<=O&&O<=44843||44845<=O&&O<=44871||44873<=O&&O<=44899||44901<=O&&O<=44927||44929<=O&&O<=44955||44957<=O&&O<=44983||44985<=O&&O<=45011||45013<=O&&O<=45039||45041<=O&&O<=45067||45069<=O&&O<=45095||45097<=O&&O<=45123||45125<=O&&O<=45151||45153<=O&&O<=45179||45181<=O&&O<=45207||45209<=O&&O<=45235||45237<=O&&O<=45263||45265<=O&&O<=45291||45293<=O&&O<=45319||45321<=O&&O<=45347||45349<=O&&O<=45375||45377<=O&&O<=45403||45405<=O&&O<=45431||45433<=O&&O<=45459||45461<=O&&O<=45487||45489<=O&&O<=45515||45517<=O&&O<=45543||45545<=O&&O<=45571||45573<=O&&O<=45599||45601<=O&&O<=45627||45629<=O&&O<=45655||45657<=O&&O<=45683||45685<=O&&O<=45711||45713<=O&&O<=45739||45741<=O&&O<=45767||45769<=O&&O<=45795||45797<=O&&O<=45823||45825<=O&&O<=45851||45853<=O&&O<=45879||45881<=O&&O<=45907||45909<=O&&O<=45935||45937<=O&&O<=45963||45965<=O&&O<=45991||45993<=O&&O<=46019||46021<=O&&O<=46047||46049<=O&&O<=46075||46077<=O&&O<=46103||46105<=O&&O<=46131||46133<=O&&O<=46159||46161<=O&&O<=46187||46189<=O&&O<=46215||46217<=O&&O<=46243||46245<=O&&O<=46271||46273<=O&&O<=46299||46301<=O&&O<=46327||46329<=O&&O<=46355||46357<=O&&O<=46383||46385<=O&&O<=46411||46413<=O&&O<=46439||46441<=O&&O<=46467||46469<=O&&O<=46495||46497<=O&&O<=46523||46525<=O&&O<=46551||46553<=O&&O<=46579||46581<=O&&O<=46607||46609<=O&&O<=46635||46637<=O&&O<=46663||46665<=O&&O<=46691||46693<=O&&O<=46719||46721<=O&&O<=46747||46749<=O&&O<=46775||46777<=O&&O<=46803||46805<=O&&O<=46831||46833<=O&&O<=46859||46861<=O&&O<=46887||46889<=O&&O<=46915||46917<=O&&O<=46943||46945<=O&&O<=46971||46973<=O&&O<=46999||47001<=O&&O<=47027||47029<=O&&O<=47055||47057<=O&&O<=47083||47085<=O&&O<=47111||47113<=O&&O<=47139||47141<=O&&O<=47167||47169<=O&&O<=47195||47197<=O&&O<=47223||47225<=O&&O<=47251||47253<=O&&O<=47279||47281<=O&&O<=47307||47309<=O&&O<=47335||47337<=O&&O<=47363||47365<=O&&O<=47391||47393<=O&&O<=47419||47421<=O&&O<=47447||47449<=O&&O<=47475||47477<=O&&O<=47503||47505<=O&&O<=47531||47533<=O&&O<=47559||47561<=O&&O<=47587||47589<=O&&O<=47615||47617<=O&&O<=47643||47645<=O&&O<=47671||47673<=O&&O<=47699||47701<=O&&O<=47727||47729<=O&&O<=47755||47757<=O&&O<=47783||47785<=O&&O<=47811||47813<=O&&O<=47839||47841<=O&&O<=47867||47869<=O&&O<=47895||47897<=O&&O<=47923||47925<=O&&O<=47951||47953<=O&&O<=47979||47981<=O&&O<=48007||48009<=O&&O<=48035||48037<=O&&O<=48063||48065<=O&&O<=48091||48093<=O&&O<=48119||48121<=O&&O<=48147||48149<=O&&O<=48175||48177<=O&&O<=48203||48205<=O&&O<=48231||48233<=O&&O<=48259||48261<=O&&O<=48287||48289<=O&&O<=48315||48317<=O&&O<=48343||48345<=O&&O<=48371||48373<=O&&O<=48399||48401<=O&&O<=48427||48429<=O&&O<=48455||48457<=O&&O<=48483||48485<=O&&O<=48511||48513<=O&&O<=48539||48541<=O&&O<=48567||48569<=O&&O<=48595||48597<=O&&O<=48623||48625<=O&&O<=48651||48653<=O&&O<=48679||48681<=O&&O<=48707||48709<=O&&O<=48735||48737<=O&&O<=48763||48765<=O&&O<=48791||48793<=O&&O<=48819||48821<=O&&O<=48847||48849<=O&&O<=48875||48877<=O&&O<=48903||48905<=O&&O<=48931||48933<=O&&O<=48959||48961<=O&&O<=48987||48989<=O&&O<=49015||49017<=O&&O<=49043||49045<=O&&O<=49071||49073<=O&&O<=49099||49101<=O&&O<=49127||49129<=O&&O<=49155||49157<=O&&O<=49183||49185<=O&&O<=49211||49213<=O&&O<=49239||49241<=O&&O<=49267||49269<=O&&O<=49295||49297<=O&&O<=49323||49325<=O&&O<=49351||49353<=O&&O<=49379||49381<=O&&O<=49407||49409<=O&&O<=49435||49437<=O&&O<=49463||49465<=O&&O<=49491||49493<=O&&O<=49519||49521<=O&&O<=49547||49549<=O&&O<=49575||49577<=O&&O<=49603||49605<=O&&O<=49631||49633<=O&&O<=49659||49661<=O&&O<=49687||49689<=O&&O<=49715||49717<=O&&O<=49743||49745<=O&&O<=49771||49773<=O&&O<=49799||49801<=O&&O<=49827||49829<=O&&O<=49855||49857<=O&&O<=49883||49885<=O&&O<=49911||49913<=O&&O<=49939||49941<=O&&O<=49967||49969<=O&&O<=49995||49997<=O&&O<=50023||50025<=O&&O<=50051||50053<=O&&O<=50079||50081<=O&&O<=50107||50109<=O&&O<=50135||50137<=O&&O<=50163||50165<=O&&O<=50191||50193<=O&&O<=50219||50221<=O&&O<=50247||50249<=O&&O<=50275||50277<=O&&O<=50303||50305<=O&&O<=50331||50333<=O&&O<=50359||50361<=O&&O<=50387||50389<=O&&O<=50415||50417<=O&&O<=50443||50445<=O&&O<=50471||50473<=O&&O<=50499||50501<=O&&O<=50527||50529<=O&&O<=50555||50557<=O&&O<=50583||50585<=O&&O<=50611||50613<=O&&O<=50639||50641<=O&&O<=50667||50669<=O&&O<=50695||50697<=O&&O<=50723||50725<=O&&O<=50751||50753<=O&&O<=50779||50781<=O&&O<=50807||50809<=O&&O<=50835||50837<=O&&O<=50863||50865<=O&&O<=50891||50893<=O&&O<=50919||50921<=O&&O<=50947||50949<=O&&O<=50975||50977<=O&&O<=51003||51005<=O&&O<=51031||51033<=O&&O<=51059||51061<=O&&O<=51087||51089<=O&&O<=51115||51117<=O&&O<=51143||51145<=O&&O<=51171||51173<=O&&O<=51199||51201<=O&&O<=51227||51229<=O&&O<=51255||51257<=O&&O<=51283||51285<=O&&O<=51311||51313<=O&&O<=51339||51341<=O&&O<=51367||51369<=O&&O<=51395||51397<=O&&O<=51423||51425<=O&&O<=51451||51453<=O&&O<=51479||51481<=O&&O<=51507||51509<=O&&O<=51535||51537<=O&&O<=51563||51565<=O&&O<=51591||51593<=O&&O<=51619||51621<=O&&O<=51647||51649<=O&&O<=51675||51677<=O&&O<=51703||51705<=O&&O<=51731||51733<=O&&O<=51759||51761<=O&&O<=51787||51789<=O&&O<=51815||51817<=O&&O<=51843||51845<=O&&O<=51871||51873<=O&&O<=51899||51901<=O&&O<=51927||51929<=O&&O<=51955||51957<=O&&O<=51983||51985<=O&&O<=52011||52013<=O&&O<=52039||52041<=O&&O<=52067||52069<=O&&O<=52095||52097<=O&&O<=52123||52125<=O&&O<=52151||52153<=O&&O<=52179||52181<=O&&O<=52207||52209<=O&&O<=52235||52237<=O&&O<=52263||52265<=O&&O<=52291||52293<=O&&O<=52319||52321<=O&&O<=52347||52349<=O&&O<=52375||52377<=O&&O<=52403||52405<=O&&O<=52431||52433<=O&&O<=52459||52461<=O&&O<=52487||52489<=O&&O<=52515||52517<=O&&O<=52543||52545<=O&&O<=52571||52573<=O&&O<=52599||52601<=O&&O<=52627||52629<=O&&O<=52655||52657<=O&&O<=52683||52685<=O&&O<=52711||52713<=O&&O<=52739||52741<=O&&O<=52767||52769<=O&&O<=52795||52797<=O&&O<=52823||52825<=O&&O<=52851||52853<=O&&O<=52879||52881<=O&&O<=52907||52909<=O&&O<=52935||52937<=O&&O<=52963||52965<=O&&O<=52991||52993<=O&&O<=53019||53021<=O&&O<=53047||53049<=O&&O<=53075||53077<=O&&O<=53103||53105<=O&&O<=53131||53133<=O&&O<=53159||53161<=O&&O<=53187||53189<=O&&O<=53215||53217<=O&&O<=53243||53245<=O&&O<=53271||53273<=O&&O<=53299||53301<=O&&O<=53327||53329<=O&&O<=53355||53357<=O&&O<=53383||53385<=O&&O<=53411||53413<=O&&O<=53439||53441<=O&&O<=53467||53469<=O&&O<=53495||53497<=O&&O<=53523||53525<=O&&O<=53551||53553<=O&&O<=53579||53581<=O&&O<=53607||53609<=O&&O<=53635||53637<=O&&O<=53663||53665<=O&&O<=53691||53693<=O&&O<=53719||53721<=O&&O<=53747||53749<=O&&O<=53775||53777<=O&&O<=53803||53805<=O&&O<=53831||53833<=O&&O<=53859||53861<=O&&O<=53887||53889<=O&&O<=53915||53917<=O&&O<=53943||53945<=O&&O<=53971||53973<=O&&O<=53999||54001<=O&&O<=54027||54029<=O&&O<=54055||54057<=O&&O<=54083||54085<=O&&O<=54111||54113<=O&&O<=54139||54141<=O&&O<=54167||54169<=O&&O<=54195||54197<=O&&O<=54223||54225<=O&&O<=54251||54253<=O&&O<=54279||54281<=O&&O<=54307||54309<=O&&O<=54335||54337<=O&&O<=54363||54365<=O&&O<=54391||54393<=O&&O<=54419||54421<=O&&O<=54447||54449<=O&&O<=54475||54477<=O&&O<=54503||54505<=O&&O<=54531||54533<=O&&O<=54559||54561<=O&&O<=54587||54589<=O&&O<=54615||54617<=O&&O<=54643||54645<=O&&O<=54671||54673<=O&&O<=54699||54701<=O&&O<=54727||54729<=O&&O<=54755||54757<=O&&O<=54783||54785<=O&&O<=54811||54813<=O&&O<=54839||54841<=O&&O<=54867||54869<=O&&O<=54895||54897<=O&&O<=54923||54925<=O&&O<=54951||54953<=O&&O<=54979||54981<=O&&O<=55007||55009<=O&&O<=55035||55037<=O&&O<=55063||55065<=O&&O<=55091||55093<=O&&O<=55119||55121<=O&&O<=55147||55149<=O&&O<=55175||55177<=O&&O<=55203?c:9757==O||9977==O||9994<=O&&O<=9997||127877==O||127938<=O&&O<=127940||127943==O||127946<=O&&O<=127948||128066<=O&&O<=128067||128070<=O&&O<=128080||128110==O||128112<=O&&O<=128120||128124==O||128129<=O&&O<=128131||128133<=O&&O<=128135||128170==O||128372<=O&&O<=128373||128378==O||128400==O||128405<=O&&O<=128406||128581<=O&&O<=128583||128587<=O&&O<=128591||128675==O||128692<=O&&O<=128694||128704==O||128716==O||129304<=O&&O<=129308||129310<=O&&O<=129311||129318==O||129328<=O&&O<=129337||129341<=O&&O<=129342||129489<=O&&O<=129501?d:127995<=O&&O<=127999?v:8205==O?g:9792==O||9794==O||9877<=O&&O<=9878||9992==O||10084==O||127752==O||127806==O||127859==O||127891==O||127908==O||127912==O||127979==O||127981==O||128139==O||128187<=O&&O<=128188||128295==O||128300==O||128488==O||128640==O||128658==O?x:128102<=O&&O<=128105?p:l}return this.nextBreak=function(e,r){if(void 0===r&&(r=0),r<0)return 0;if(r>=e.length-1)return e.length;for(var n,t,i=B(b(e,r)),u=[],f=r+1;f<e.length;f++)if(t=f-1,!(55296<=(n=e).charCodeAt(t)&&n.charCodeAt(t)<=56319&&56320<=n.charCodeAt(t+1)&&n.charCodeAt(t+1)<=57343)){var o=B(b(e,f));if(k(i,u,o))return f;u.push(o)}return e.length},this.splitGraphemes=function(e){for(var r,n=[],t=0;(r=this.nextBreak(e,t))<e.length;)n.push(e.slice(t,r)),t=r;return t<e.length&&n.push(e.slice(t)),n},this.iterateGraphemes=function(e){var r=0,n={next:function(){var n,t;return(t=this.nextBreak(e,r))<e.length?(n=e.slice(r,t),r=t,{value:n,done:!1}):r<e.length?(n=e.slice(r),r=e.length,{value:n,done:!1}):{value:void 0,done:!0}}.bind(this)};return"undefined"!=typeof Symbol&&Symbol.iterator&&(n[Symbol.iterator]=function(){return n}),n},this.countGraphemes=function(e){for(var r,n=0,t=0;(r=this.nextBreak(e,t))<e.length;)t=r,n++;return t<e.length&&n++,n},this}
var splitter = new GraphemeSplitter();
var splat = splitter.splitGraphemes("हैंI");
var reversed = splat.reverse().join("")
console.log(reversed);
答案 1 :(得分:3)
主要问题是找到一种将“हैं”捕获为单个字符而不是3个单独字符的方法。
encodeURIComponent
实际上将"हैंI"
转换为"%E0%A4%B9%E0%A5%88%E0%A4%82I"
,使用正则表达式更容易处理。
使用String#match,Array#join,encodeURIComponent和decodeURIComponent
const res = decodeURIComponent(encodeURIComponent("हैंI")
.match(/(\%[A-F0-9]{2})+|./g)
.reverse().join(''))
console.log(res);
但是,如果这些特殊字符中的至少两个并排在一起,则会失败。由于"%E0%A4%B9%E0%A5%88%E0%A4%82"
有9个匹配项,因此我们可以要求%
的两个字符组成的连续序列必须匹配9个。
const res = decodeURIComponent(encodeURIComponent("हैंहैंI")
.match(/(\%[A-F0-9]{2}){9}|./g)
.reverse().join(''))
console.log(res);
function reverse(str) {
return decodeURIComponent(encodeURIComponent(str)
.match(/(\%[A-F0-9]{2}){9}|./g)
.reverse().join(''))
}
console.log(reverse("हैंI"))
console.log(reverse("AहैंI"))
console.log(reverse("IAहैं"))
console.log(reverse("हैंहैंI"))
console.log(reverse("ABहैंहैंIA"))
console.log(reverse("ABहैंIU90QहैंIA"))
我也尝试了这种逻辑,但是在map
之后反转字符给出了错误的结果。但是,我很肯定,如果有一种方法可以自动捕获特殊字符而不是手动捕获特殊字符,则该方法最终可以很好地工作(请参见最后一个示例)。
const res1 = String.fromCodePoint(...[..."हैंI"].map(c=>c.charCodeAt()));
const res2 = String.fromCodePoint(...[..."हैंI"].map(c=>c.charCodeAt()).reverse());
console.log(res1, res2);
//however it works when they are put in the right order
const arr = [..."हैंI"]
.map(c=>c.codePointAt())
.filter(a=>a!==undefined);
console.log(arr);
const res = String.fromCodePoint(...arr.splice(arr.length-1,1), ...arr)
console.log(res);