This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 7 years ago.
I want to split a string in Java script using
str.split([separator[, limit]])
I want to split it by a space, but ' ' did not work.
What should I use?
This question has previously been asked here: How do I split a string, breaking at a particular character?
In your specific case, based on what you provided, it appears you are attempting to split on nothing. '' instead of an actual space ' '.
To replace the spaces by commas:
var str = "How are you doing today?";
var res = str.split(" ");
//Output: How,are,you,doing,today?
Like described here: http://www.w3schools.com/jsref/jsref_split.asp
Another option is to use str.replace:
Var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/gi, "red");
//Output: Mr red has a red house and a red car
Like described here: http://www.w3schools.com/jsref/jsref_replace.asp
But what you may actually wanted:
var str = "Test[separator[, limit]]Test";
var res = str.split("[separator[, limit]]").join(" ");
//Output: Test Test
Related
This question already has answers here:
JavaScript split String with white space
(8 answers)
Closed last year.
In my input field I have sometimes words that contain apostrophe or need to be searched using quotes.
Example of searched word: "de l'article".
I need to separate them by space, so I will have "de" and "l'article".
I tried:
let word = document.getElementById("searchedWord").value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let x = new RegExp("(" + word.replace(/\s+[']+/g, "|") + ")", "ig");
It doesn't seem to work properly. Can anyone help me write this regular expression?
You should check out the split function :)
for example in order to split on every space
let x = "This is an example";
const myArray = x.split(' ');
let word = myArray[1]
word = 'This'
myArray = [This,is,an,example]
This question already has answers here:
Remove ALL white spaces from text
(14 answers)
Closed 4 years ago.
var name="james mark";
var name="james " ;
so how to trim both the names occur any condition from this anytime in javascript
Output:
james mark
james
You can replace all instances of repeated white space with a single space and also trim all leading and trailing white space:
name.replace(/\s+/g,' ');
name.replace(/^\s+|\s+$/g,''); // or use .trim()
for more information you should read the description on the .replace() and .trim() functions on MDN
Try with:
var name="james mark";
var j = names.split(' ');
alert(j[0]);
//The otput: James (without the blank spaces)
This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'
This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 7 years ago.
this is a test. this is one more test. and this also new test.
This is test. This is one more test. And this is new test.
You don't need jQuery for this. You can use Javascript string and array functions.
Split the string by ., to separate different sentences
Trim each sentence
Capitalize first letter
Join by .
var str = 'this is a test. this is one more test. and this also new test.';
var newStr = str.split('.').map(function(el) {
el = el.trim();
return el.substr(0, 1).toUpperCase() + el.substr(1);
}).join('. ');
alert(newStr.trim());
This question already has answers here:
How to split newline
(13 answers)
Closed 7 years ago.
We are Developing phonegap application.We get Data form CSV file. It's look like this
We need Data Split into two strings Like
String1
String2
We tried like this but We don't have luck so Please guide me
var split = string.split('\r\n');
Please help me
try this:
var split = string.split(/\n/);
Replace the new line characters with space and then split the string with space
string.replace( /\n/g, " " ).split(" ");
UPDATE:
var string1=string.substring(0,string.indexOf("TOTALAMOUNT"));
var string2=string.substring(string.indexOf("TOTALAMOUNT"),string.length);
Or if your string contains \n then:
var string1=string.substring(0,string.indexOf("\n"));
var string2=string.substring(string.indexOf("\n"),string.length);
alert(string1);
alert(string
Fiddle