I want to split a string based on the delimiters " " and "," (including quotation marks). How would I be able to do this? I tried doing this:
var str = '"String","String2" "String3"';
str = str.split('" "', '","');
console.log(str);
But it didn't work. I was expecting this console output:
["String", "String2", "String3"]
But I got:
[]
How do I split a string based on two delimiters? Is it possible?
let str = '"String","String2" "String3"';
str = str.split(/ |,/);
console.log(str);
let str2 = '"String","String2" "String3"';
str2 = str2.split(/" "|","|"|\n/).filter(Boolean);
console.log(str2);
let str3 = '"String","String2" "String3"';
str3 = str3.match(/[\w\d]+/g);
console.log(str3);
You can specify regex or array as delimiter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
try below solution
var str = '"String","String2" "String3"';
str = str.match(/\w+|"[^"]+"/g)
str = str.map(function(x){return x.replace(/"/g, '');});
console.log('Final str : ', str);
You can use regex: str = str.split(/,| /)
console.log("a,b c".split(/,| /));
Related
var str = "Demo Docs Version 1.0.1";
var got = str.split(" ").splice(-1)[0]
str = str.replace(got, "testing");
console.log(str);
I can replace last word if there is space in between but how can i replace my last word when there _ in string ?
var str = "Demo-Docs-Version-1-0-1";
var got = str.split(" ").splice(-1)[0]
str = str.replace(got, "testing");
console.log(str);
What about this?
str = str.replace(/(?!.*(\s|_)).*/, "testing")
var str = 'asd-0.testing';
var regex = /asd-(\d)\.\w+/;
str.replace(regex, 1);
That replaces the entire string str with 1. I want it to replace the matched substring instead of the whole string. Is this possible in Javascript?
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
str = str.replace(regex, "$11$2");
console.log(str);
Or if you're sure there won't be any other digits in the string:
var str = 'asd-0.testing';
var regex = /\d/;
str = str.replace(regex, "1");
console.log(str);
using str.replace(regex, $1);:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
if (str.match(regex)) {
str = str.replace(regex, "$1" + "1" + "$2");
}
Edit: adaptation regarding the comment
I would get the part before and after what you want to replace and put them either side.
Like:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
var matches = str.match(regex);
var result = matches[1] + "1" + matches[2];
// With ES6:
var result = `${matches[1]}1${matches[2]}`;
I think the simplest way to achieve your goal is this:
var str = 'asd-0.testing';
var regex = /(asd-)(\d)(\.\w+)/;
var anyNumber = 1;
var res = str.replace(regex, `$1${anyNumber}$3`);
How can I convert currency string like this $123,456,78.3 to number like this 12345678.3
I tried to do using this pattern
let str = "$123,456,78.3";
let newStr = str.replace(/\D/g, '');
but it replaces . too.
var str = "$123,456,78.3";
var newStr = Number(str.replace(/[^0-9.-]+/g,""));
Use a lowercase d, and put it into a negated character group along with the ..
let str = "$123,456,78.3";
let newStr = str.replace(/[^\d.]/g, '');
console.log(newStr)
You can use a negative character group:
"$123,456,78.3".replace(/[^\d.]/g, "")
Basically I need to get the number of words in an input field. So the approach is to trim the leading and trailing spaces and also to limit the remaining spaces within the string to 1. So that I'll be able to get the number of words. Below is my code to do this.
E.g.
input value:
" Robert Neil Cook "
Expected output:
3 //"Robert Neil Cook"
This is what I tried.
var str = $.trim( $('#inval').val() );
var Fstr = str.split(' ').length;
console.log(fstr);
You can use below custom function for count words
function countWords(s){
s = s.replace(/(^\s*)|(\s*$)/gi,"");//exclude start and end white-space
s = s.replace(/[ ]{2,}/gi," ");//2 or more space to 1
s = s.replace(/\n /,"\n"); // exclude newline with a start spacing
return s.split(' ').length;
}
alert(countWords("How are you?"));
Try below :
str= " Robert Neil Cook " ;
str = $.trim(str.replace(/ +(?= )/g,'')); // replace multiple spaces to single space
console.log(str)
var words = str.split(' '); // count the no of words using .split() method
alert(words.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
You can use .match(/\S+/g):
var str = " Robert Neil Cook ";
var arr = str.match(/\S+/g);
var newstr = arr.join()
console.log('length::::>', arr.length);
console.log('newstr::::>', newstr);
This .match(/\S+/g) would return you the words without any space as an array and you can use length property of it.
Try this code..
var str = $.trim( $('#inval').val() );
var words = str.split(' ').filter(v=>v!='').length;
You can Haresh Vidja's function more compact like this
function countWords(s){
return s.trim().replace(/[ ]{2,}/gi," ").split(" ").length;
}
console.log(countWords("some words here")
Please have a look attached snippet.
var str = $.trim( $('#inval').val() );
str1 = str.replace(/ +(?= )/g,'')
var fstr = str1.split(' ').length;
console.log(fstr);
console.log(str1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value=" Robert Neil Cook " id="inval">
Very simple solution, try this
var str = " Robert Neil Cook ";
str = str.trim();
var str = str.split(' ').join('');
var fstr = str.split(' ').length;
console.log(fstr);
var str = 'asd-0.testing';
var regex = /asd-(\d)\.\w+/;
str.replace(regex, 1);
That replaces the entire string str with 1. I want it to replace the matched substring instead of the whole string. Is this possible in Javascript?
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
str = str.replace(regex, "$11$2");
console.log(str);
Or if you're sure there won't be any other digits in the string:
var str = 'asd-0.testing';
var regex = /\d/;
str = str.replace(regex, "1");
console.log(str);
using str.replace(regex, $1);:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
if (str.match(regex)) {
str = str.replace(regex, "$1" + "1" + "$2");
}
Edit: adaptation regarding the comment
I would get the part before and after what you want to replace and put them either side.
Like:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
var matches = str.match(regex);
var result = matches[1] + "1" + matches[2];
// With ES6:
var result = `${matches[1]}1${matches[2]}`;
I think the simplest way to achieve your goal is this:
var str = 'asd-0.testing';
var regex = /(asd-)(\d)(\.\w+)/;
var anyNumber = 1;
var res = str.replace(regex, `$1${anyNumber}$3`);