Replace string which is between two characters [duplicate] - javascript

This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Closed 3 years ago.
I am having the following string 4 (10.3%) and would like to receive 4.
I tried:
let s = "4 (10.3%)"
s.replace("/\s+\((.*?)\)", "");
console.log(s)
However, I still get the initial string back.
Any suggestion what I am doing wrong?
I appreciate your replies!

Use this instead of regex
let s = "4 (10.3%)"
console.log(s.substr(0, s.indexOf(' ')))
Using split
let s = "4 (10.3%)"
console.log(s.split(" ")[0])

Related

How to format string in JS? [duplicate]

This question already has answers here:
JavaScript .replace only replaces first Match [duplicate]
(7 answers)
Closed 6 years ago.
I would like to have the result :
28,12,2016
From this string "28/12/2016"
I tried :
("28/12/2016").replace('/',',');
==>"28,12/2016"
I don't know how to delete the second /and the " "
use split and join method
var a="28/12/2016";
var ans=a.split("/").join(",");
console.log(ans);

Replacing “index” array string using Javascript regex [duplicate]

This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 6 years ago.
with the following code can get the 1 in the string.
var match = /[0-9]+/.exec('[1][2]');
console.log(match);
How do i get the 2 and not the 1 ?
Try with this regex
console.log(/(\[[0-9]\])+/.exec('[1][2]'));
I think it's only a matter of escaping the square brackets

How to separate words within a long string [duplicate]

This question already has answers here:
Split by Caps in Javascript
(4 answers)
Closed 3 years ago.
I have this string variable
var mySentence = "EverythingIsAwesome";
I want it to output: "Everything Is Awesome"
I tried doing mySentence.split(" "); but it stays the same. Then I tried: mySentence.split(" ").join(" "); but it still keeps the sentence together.
Can anyone help me figure out what I'm doing wrong? Many thanks in advance!
You should use match() like this:
mySentence.match(/[A-Z][a-z]+/g);
You will get this array: ['Everything', 'Is', 'Awesome']
To turn it back into a string simply: mySentence.join("");

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

how to split into character group? [duplicate]

This question already has answers here:
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 9 years ago.
I want to split string
"abcdefgh"
to
"ab","cd","ef","gh"
using javascript split()
"abcdefgh".split(???)
Can you please help ?
Instead of split, try match:
var text = "abcdefgh";
print(text.match(/../g));
prints:
ab,cd,ef,gh
as you can see on Ideone.

Categories

Resources