473,404 Members | 2,137 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,404 software developers and data experts.

javascript replace not working for me with global tag

31
All,

I cannot get my javascript code to work with the global flag for replace.

Here is the code I have (I am trying to find a certain character and replace it with a new line)
Expand|Select|Wrap|Line Numbers
  1. strTemp = splitString(strTemp, 80, '*');
  2.  
  3. function splitString(stringToSplit, sizeOfSplit, extraCharacterToSplit)
  4. {
  5.     var i =  stringToSplit.length /parseInt(sizeOfSplit);
  6.     var sTemp;
  7.     var sFinalString = "";
  8.  
  9.     if (extraCharacterToSplit != "")
  10.     {
  11.         stringToSplit = stringToSplit.replace('/' + extraCharacterToSplit + '/g', extraCharacterToSplit + '\n');
  12.     }
  13.  
  14.     for (i = 0; i < Math.floor(stringToSplit.length / parseInt(sizeOfSplit)); i++)
  15.     {
  16.         sTemp = stringToSplit.substr(i * sizeOfSplit, sizeOfSplit);
  17.         sFinalString = sFinalString + sTemp + "\n"; 
  18.     }
  19.     sTemp = stringToSplit.substr(i * sizeOfSplit, sizeOfSplit);
  20.     sFinalString = sFinalString + sTemp; 
  21.  
  22.     return sFinalString;
  23. }
Any ideas what I am doing wrong?

Thx
jonpfl
Aug 13 '07 #1
4 2142
pbmods
5,821 Expert 4TB
Heya, jonpfl. Welcome (back) to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 13 '07 #2
acoder
16,027 Expert Mod 8TB
Any ideas what I am doing wrong?
See link on how to use regular expressions in Javascript.

Don't use a string unless you're going to use the RegExp object.
Aug 13 '07 #3
jonpfl
31
See link on how to use regular expressions in Javascript.

Don't use a string unless you're going to use the RegExp object.
I changed my code to the following and it still does not work with a siimple string passed in :

Expand|Select|Wrap|Line Numbers
  1. function splitStringJon(stringToSplit, sizeOfSplit, extraCharacterToSplit)
  2. {
  3.     var nStringLength = stringToSplit.length;;
  4.     var sTemp;
  5.     var nCurrentPos, nOriginalPos, nSplitSize;
  6.     var sFinalString = "";
  7.  
  8.     // This is used to replace the extraCharacterToSplit with a newline
  9.     if (extraCharacterToSplit != "")
  10.     {
  11.         var re = new RegExp(stringToSplit, "g");
  12.         stringToSplit = stringToSplit.replace(re, extraCharacterToSplit + '\n');
  13.     }
Can you see a problem with this?

Thx
jonpfl
Aug 13 '07 #4
pbmods
5,821 Expert 4TB
Heya, jonpfl.
The first parameter to the RegExp() constructor is the regular expression that you want to use, not the string that you want to process.
Aug 13 '07 #5

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

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.