473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trim/Replace using javascript?

nirmalsingh
218 100+
hi all,
i am having a string variable like,
var strValue='hai i am "Nirmal" how are you? ""- ^ !++&%$";
i need the result like "haiiamNirmalhowareyou?"
how could i wrote code for this???
Feb 6 '07 #1
8 6345
acoder
16,027 Expert Mod 8TB
I am assuming you want only alphabetic characters. In that case, you can use a regular expression to remove all non-alphabetic characters:
Expand|Select|Wrap|Line Numbers
  1. str.replace(/[^a-z]/ig,"");
Feb 6 '07 #2
nirmalsingh
218 100+
I am assuming you want only alphabetic characters. In that case, you can use a regular expression to remove all non-alphabetic characters:
Expand|Select|Wrap|Line Numbers
  1. str.replace(/[^a-z]/ig,"");
i tried as
strValue=str.replace(/[^a-z]/ig,"");
but
syntax error occured
Feb 6 '07 #3
acoder
16,027 Expert Mod 8TB
What was the syntax error? It works fine for me. If you still have problems, post the rest of your code.
Feb 6 '07 #4
nirmalsingh
218 100+
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript" type="text/javascript">
  4. function general()
  5. {
  6. var treedata="1^0^Top|106^1^Films|395^1^fd|432^1^12121|131^1^Papiers argentiques|2^1^Présentation|182^1^Chimie - Retouche|63^1^Labo|33^1^Montage - Encadrement|88^1^Classement|203^1^Accessoires prise de vue|247^1^lampes - Piles|78^1^Numérique|3^2^Books|10^2^Feuillets pour books|21^2^Serviettes porte books|22^2^Boites|25^2^Albums|31^2^Chemises|32^2^Petits accessoires|4^3^Books à anneaux|5^3^Books à feuillets soudés|6^3^Books à spirale non rechargeables|7^3^Books à spirale rechargeables|8^3^Classeurs|9^3^Books a faisceaux|11^10^Feuillets polyester|15^10^Feuillets polypropylène|19^10^Feuillets vinyle|20^10^Feuillets carton|12^11^Feuillets classeur|13^11^Feuillets pour Printibook|14^11^Feuillets pour Pampabook 143P|16^15^Feuillets classeur|17^15^Feuillets pour 143/147/153/149|18^15^Faisceaux|23^22^Boite portfolio|24^22^Boites cloche|26^25^Albums classiques|27^25^Albums à spirale|28^25^Albums cuir|29^25^Livres d'or|30^25^Pratic System|34^33^Cartons - Mousse - Papiers|39^33^Supports adhésifs|42^33^Adhésifs - Colles - Coins|50^33^Chevalets - Accrochage|53^33^Outils de coupe|58^33^Cadres - Sous-verre|35^34^Carton noir âme noire|36^34^Carton sans acide|37^34^Contrecollés|38^34^Cartons et papiers divers|40^39^Carton adhésif|41^39^Mousse adhésive|43^42^Adhésif à froid|46^42^Adhésif à chaud|47^42^Colles|48^42^Coins - Bandes de montage|49^42^Rubans adhésifs|44^43^Système 3M ATG|45^43^Adhésif double face - Pastilles|51^50^Chevalets carton|52^50^Attaches - Adhésifs - Velcro|54^53^Cisailles|55^53^Cutters - Ciseaux|56^53^Cutters pour passe partout|57^53^Règles - Tapis de coupe|59^58^Cadres bois|60^58^Cadres aluminium|61^58^Cadres résine|62^58^Sous verres|64^63^Agrandisseurs|67^63^Margeurs|70^63^Compte pose|77^63^Vérificateurs de mise au point|71^63^Cuves de développement|72^63^Cuvettes - Eviers - Flaconnages|75^63^Lavage - Essorage - Séchage|76^63^Petit matériel|65^64^Agrandisseurs|66^64^Objectifs|68^67^Margeurs|69^67^Tireuses - Accessoires|73^72^Cuvettes - Eviers|74^72^Flacons - Éprouvettes|79^78^Papiers jet d'encre|86^78^Cartes mémoire|87^78^Classement CD|80^79^Bergger|81^79^Canson|82^79^Ilford|83^79^Permajet|84^79^Tetenal|85^79^Avery|89^88^Feuillets de classement|92^88^Montures|97^88^Pochettes individuelles|101^88^Pochettes de livraison|102^88^Pochettes conservation|90^89^Feuillets perforés|91^89^Feuillets suspendus - Tringles|93^92^Montures carton|94^92^Caches sans verre|95^92^Caches avec verre|96^92^Accessoires diapos|98^97^Pochettes vinyle|99^97^Pochettes papier cristal|100^97^Fourreaux acétate|103^102^Pochettes papier permanent|104^102^Pochettes terphane|105^102^Pochettes pour classeur|107^106^Films NB|111^106^Films négatif couleur|120^106^Films inversible couleur|129^106^Films instantanés|130^106^Films labo|108^107^135|109^107^120|110^107^4"x5"|112^111^Lumière du jour|116^111^Lumière artificielle|113^112^135|114^112^120|115^112^4"x5"|117^116^135|118^116^120|119^116^4"x5"|121^120^Lumière du jour|125^120^Lumière artificielle|122^121^135|123^121^120|124^121^4"x5"|126^125^135|127^125^120|128^125^4"x5"|132^131^Papier plastique a contraste variable|147^131^Papiers barytes à contraste variable|162^131^Papiers barytés à grade fixe|169^131^Papiers spéciaux|176^131^Papier couleur|133^132^Ton neutre|144^132^Ton chaud|134^133^AGFA - MCP 310 RC|135^133^AGFA - MCP 312 RC|136^133^ILFORD - MGD 1M|137^133^ILFORD - MGD 25M|138^133^ILFORD - MGD 44M|139^133^ILFORD - MGS 1K|140^133^ILFORD - MGS 44K|141^133^KENTMERE - VC Select|142^133^TETENAL - Vario 310|143^133^TETENAL - Vario 312|145^144^ILFORD - MGT 1M|146^144^ILFORD - MGT 44M|148^147^Ton neutre|152^147^Ton chaud|149^148^BERGGER - VNB|150^148^ILFORD - MGF 1K|151^148^ILFORD - MGF 5K|153^152^AGFA - MCC 118 FB|154^152^BERGGER - VCB|155^152^BERGGER - VCM|156^152^BERGGER - VCX|157^152^BERGGER - VCB style|158^152^ILFORD - MGW 1K|159^152^ILFORD - MGW 24K|160^152^KENTMERE - Fineprint VC|161^152^TETENAL - Baryt Vario|163^162^Bromure|166^162^Chlorobromure|164^163^BERGGER - NB Art|165^163^BERGGER - Fine Art Portrait|167^166^BERGGER - CB Art|168^166^KENTMERE - Art Classic|170^169^BERGGER - BROM 240|171^169^BERGGER - COT 320|172^169^CANSON - Arche Platine|173^169^KENTMERE - Art Document|174^169^KENTMERE - Art Luxe|175^169^KENTMERE - RC Art|177^176^D'après négatif|178^177^FUJICOLOR Cristal Archive G|179^177^FUJICOLOR Cristal Archive M|180^177^KODAK Supra Endura F|181^177^KODAK Supra Endura N|183^182^Chimie NB|197^182^Chimie couleur|198^182^Retouche|201^182^Produits chimiques de base|202^182^Produits accessoires|184^183^Révélateurs film|185^183^Révélateurs papier|189^183^Bains d'arrêt|190^183^Fixateurs|191^183^Auxiliaires de traitement|195^183^Virages|196^183^Émulsion liquide|186^185^Révélateurs ton neutre|187^185^Révélateur doux|188^185^Révélateurs tons chauds|192^191^Auxiliaires de lavage|193^191^Agents mouillants|194^191^Autres auxiliaires de traitement|199^198^Pinceaux - Lunettes|200^198^Produits de retouche|204^203^Trépied et accessoires|213^203^Eclairage|218^203^Pieds d'éclairage|221^203^Fonds et supports de fond|224^203^Accessoires studio|229^203^Filtres de prise de vue|235^203^Sacs et valises|236^203^Cellules - Flashmètres - Chartes|239^203^Petit matériel|243^203^Tables lumineuses|244^203^Loupes|205^204^Trépieds Manfrotto|206^204^Trépieds Gitzo|207^204^Monopods|208^204^Rotules Manfrotto|209^204^Rotules Gitzo|210^204^Plateaux rapides|211^204^Accesooires trépieds|212^204^Sacs pour pieds|214^213^Eclairage continu|215^213^Flash|216^213^Parapluies et supports|217^213^Réflecteurs et supports|219^218^Pieds|220^218^Accessoires|222^221^Fonds|223^221^Supports de fond|225^224^Pinces - Clamps|226^224^Accessoires pour clamp|227^224^Autopoles|228^224^Bras articulés|230^229^Porte filtres|231^229^Filtres gélatine|232^231^CC|233^231^CT|234^231^ND|237^236^Cellules - Flashmètres|238^236^Chartes|240^239^Déclencheurs|241^239^Nettoyage|242^239^Petit matériel|245^244^Compte-fils|246^244^Loupes|248^247^Lampes|262^247^Piles - Accus|249^248^Laboratoire|253^248^Projection|256^248^Prise de vue|250^249^Eclairage labo|251^249^Agrandisseur|252^249^UV|254^253^Basse tension|255^253^Miroir|257^256^Ambiance|258^256^Episcope|259^256^Pilote|260^256^Torche|261^256^Tubes flash|263^262^Piles|264^262^Accus - Chargeurs|" ;
  7. treedata =  str.replace(/[^a-z]/ig,"");
  8. alert(treedata);
  9. }
  10. </script>
  11. </head>
  12. <body onload="general()">
  13. </body>
  14. </html>[
i get the error as object expected.
here i want to trim spaces and i want it to accept all the special characters.
Feb 7 '07 #5
acoder
16,027 Expert Mod 8TB
You've not defined str. You have to set that before you can call replace. Define str to your string value then it should work fine.
Feb 7 '07 #6
vee10
141 100+
hi,
U should replace str with treedata and in ur input u have given " at the position
Expand|Select|Wrap|Line Numbers
  1. "labo|108^107^135|109^107^120|110^107^4"x5"|112^111^Lumière du jour|116^111^Lumière artificielle|"
  2.  
so the string will be closed and so u get error expected ";" i hope u understood so u r getting that error if u remove that ur problem will be solved so replace " with ' or with other character
Feb 7 '07 #7
Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text).. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhadteeth" anything besides characters should be stripped out of the inputed field.


What was the syntax error? It works fine for me. If you still have problems, post the rest of your code.
Apr 26 '07 #8
Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text).. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhadteeth" anything besides characters should be stripped out of the inputed field.

but this is not in java this is in Visual Basic.net
Apr 26 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Reply Via Newsgroup | last post by:
Folks, In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string. Can someone tell me how I can do this in...
3
by: Jon Kelling | last post by:
for all your javascript trimming needs... function trim(inputString) { if (typeof inputString != "string") return inputString; return inputString //clear leading spaces and empty lines...
3
by: mhk | last post by:
Hi, i used Trim(my_val) and its giving error in JavaScript. is it a correct way to trim. Please suggest me, thanks in advance.
5
by: Alex Vassiliev | last post by:
Hi all. Just wanted to share two handy RegEx expressions to strips leading and trailing white-space from a string, and to replace all repeated spaces, newlines and tabs with a single space. *...
12
by: Robert Mark Bram | last post by:
Hi All, I am using the following trim function: function trim (str) { return str.replace(/^\s*/g, '').replace(/\s*$/g, ''); } The problem is that this doesn't trim instances of the...
27
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?...
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.