How to print first sentence from semicolon separated string? [duplicate] - javascript

This question already has answers here:
Getting text before a character in a string in Javascript
(4 answers)
Closed 3 months ago.
I have a column in database table that saves type string in it. It saves multiple lines separated by semicolon, something like this -
this is first sentence;this is second sentence;this is third sentence
I want to print first line only, i.e. -
this is first sentence
Any idea how can I achieve this using JavaScript?

Use String.prototype.split
const string ='this is first sentence;this is second sentence;this is third sentence'
const sentences = string.split(';')
const first = sentences[0]
console.log(first)

Related

how to get second value from string in JavaScript? [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 4 months ago.
I have a string with comma now i want to get first value how can i get it?
My Code:-
var data = "0.46609362959861755, 0.25069287419319153, 0.5107838958501816, 0.26014574989676476";
console.log(data.replaceAll(',','').splice(0,2));
Thanks for your efforts!
Maybe you need this?
data.split(',')[0]
console.log(data.split(', ')[0])
Use this instead of what you did.
data.split(', ')
returns an array with every element in your string, separately, with no spaces or commas attached. The [0] after it grabs the first element of the array.

Replace multiple strings in a text file with one string using javascript [duplicate]

This question already has answers here:
Replace a line in txt file using JavaScript
(2 answers)
Closed 1 year ago.
I'm trying to replace multiple strings in a text file with one string using javascript
I want those two strings "user" and "password" must be replaced with 'replace the string'
Anyone help me here?
to add to the previous replies, you can use capture groups to back reference parts of the matched text i.e.
if you search for "user"
let newText = yourText.replace(/^((\"user\")(:".*")(.*))$/gm, '"removed"$3$4' );
if you search for "1232"
let newText = yourText.replace(/^((\".*\")(:"1232")(.*))$/gm, '"removed"$3$4' );

how to get the whole string matched in regex [duplicate]

This question already has answers here:
How do I find words starting with a specific letter?
(2 answers)
Closed 2 years ago.
I have this code
const paragraph = 'my name is bright and this is a testing interface, right.';
const regex = /\b(b)/g;
const found = paragraph.match(regex);
console.log(found);
What i want is that i want to get the whole word instead of just a single letter.
e.g the output in this code above is b which is gotten from the string bright in the paragraph but i don't just want the b but the word bright as a whole and still be able to manipulate it like make it bolder or something else. Please how do i do it and i have also checked other similar questions on stackoverflow but nothing
Assuming you only want to match words delimited by spaces, this should do the trick.
((?:\w)+)

how to parse values from comma separated values using regex for javascript [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I have
HOSTNAMEA,HOSTNAMEB,HOSTNAMEC,...
I have a third party workflow tool that can do the looping but can only use regex to parse values. I'd like to get a regex that grabs each hostname and puts into it's own variable in my workflow tool so the results will be
HOSTNAMEA
HOSTNAMEB
HOSTNAMEC
...
I'm struggling to get a regex that just grabs the text block X between the commas
ever heard of \w+ if you just want the strings between the comma, you can use .split(", ") as well
var str = "HOSTNAMEA,HOSTNAMEB,HOSTNAMEC";
var res = str.match(/\w+/g);
console.log(res.join(" "));
sample code for your help

How to replace four backslashes in a string to two [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I have a string like this coming from the server and its not working due to four backslashes. if i remove four with two its working.
URL_https~~\\\\fbcdn-sphotos-f-a.akamaihd.net\
May I know how to replace four backslashes with two as below
URL_https~~\\fbcdn-sphotos-f-a.akamaihd.net\
I tried various things but nothing worked out
i tried as follows
one:
strTest2.replace("\\\\\\\\","\\\\"
two:
strTest2 .replace(/[/\*]/, "");
Three:
strTest2.replace(/\|\|/g, "\\");
You need to store the new string created
strTest2 = strTest2.replace("\\\\\\\\","\\\\");
all of the replace methods return a new string. not alter the current string.
You need to assign the result, because strings are immutable.
The first one would have actually worked, but it only replaces the first occurrence of four backslashes. To replace all occurrences, you need to use an actual regex literal:
strTest2 = strTest2.replace(/\\\\\\\\/g,"\\\\");
You can improve readability of the above expression with a quantifier:
strTest2 = strTest2.replace(/(?:\\){4}/g,"\\\\");

Categories

Resources