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)+)
Related
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)
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
I got following code snippet, it's pretty simple,
var b = "'aa','bb'";
console.log(b.replace("'", ""));
// result is "aa','bb'"
I want replace all single quote signs with blank. So my expected output should be "aa,bb", but the actual output is "aa','bb'" neither run this code snippet in Node nor browser. Seems only the first single quote sign has been replaced.
I already got a workaround to resolve this problem by replace with regex. What I wanna know
is what happened to replace function here? I cannot figure this out.
Try using RegEx specifying the global flag (g) that matches the pattern multiple times. Please also note that, as replace() does not modify the original string you have to reassign the modified value to the variable:
var b = "'aa','bb'";
b = b.replace(/'/g, "");
console.log(b);
This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 3 years ago.
I need to split a given text by urls that it might contain, while keeping the urls-separators in the resulting array.
For example splitting this text:
"An example text that contains many links such us
http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and
link-4.com."
would result into this array:
["An example text that contains many links such us ", "http://www.link1.com", ", ", "https://www.link2.com/path?param=value", ", ", "www.link3.com", " and ", "link-4.com", "."]
I tried to use String.protoype.split() with a regular expression, but it's not working as it contains unwanted parts of the urls themselves:
var text = "An example text that contains many links such us http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and link-4.com.";
console.log(text.split(/((https?:\/\/)|([\w-]{2,}[.])+([\S]{2,})[^\s|,!$\^\*;:{}`()])+/ig));
EDIT
This question is different than the suggested ones, my purpose is not to check if a url is valid or not, but to find a regular expression susceptible to be used in the split method, and that splits correctly the text.
As for splitting a text by regex, it is already used in the snippet sample. What is proposed in the suggested question is more general, and what I am looking for is more specific to urls.
it's not ideal and it would be hard to find or create perfect regex for it that you going to test all cases but you can quickly write something like this:
var text2 = "An example text that contains many links such us http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and link-4.com.";
text2
.split(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/ig)
.filter(Boolean)
.filter((x)=>{ return x.indexOf('.')>0 })
This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 5 years ago.
I'm developing a chrome and firefox extension and i'm stuck with matching a certain tag and content inside of that. Can you please help me out?
Code:
[QUOTE=UserAdmin;22061013]
[SIZE="4"]
[LEFT]
[COLOR="DarkGreen"] Sample text goes here [/COLOR]
[/LEFT]
[/SIZE]
[/QUOTE]
Here i'd like to match beginning of [QUOTE= because everything what comes after that will be totally different each time and finally by the closing tag of [/QUOTE]
I'm not a regex expert and here is what i've came up with:
const regex = /^(\[QUOTE=)/;
const str = "[QUOTE=UserAdmin;22061013][SIZE="4"] [LEFT] [COLOR="DarkGreen"] Sample text goes here [/COLOR] [/LEFT] [/SIZE][/QUOTE]";
It successfully matched as below but i'm not sure this is the correct way of doing it:
If i can have a regex code to match whatever inside the [QUOTE=]....[/QUOTE] tag and save it to later use would be highly appreciated.
Online regex fiddle link
Try this
\[QUOTE=[\s\S]+\[\/QUOTE\]
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I'm trying to use RegEx to select all strings between two dollar signs.
text = text.replace(/\$.*\$/g, "meow");
I'm trying to turn all text between two dollar signs into "meow" (placeholder).
EDIT:
Original question changed because the solution was too localized, but the accepted answer is useful information.
That's pretty close to what you want, but it will fail if you have multiple pairs of $text$ in your string. If you make your .* repeater lazy, it will fix that. E.g.,
text = text.replace(/\$.*?\$/g, "meow");
I see one problem: if you have more than one "template" like
aasdasdsadsdsa $a$ dasdasdsd $b$ asdasdasdsa
your regular expression will consider '$a$ dasdasdsd $b$' as a text between two dolar signals. you can use a less specific regular expression like
/\$[^$]*\$/g
to consider two strings in this example