Suppose this string:
b*any string here*
In case this exists, I want to replace b* at the beginning to <b>, and the * at the end to </b> (Disregard the backslash I need it for escaping on SO site).
Moreover, there might be more then one match:
b*any string here* and after that b*string b*.
These cases should not be handled:
b*foo bar
foo bar*
bb*foo bar* (b is not after a whitespace or beginning of string).
I've gotten this far:
(?<=b\*)(.*?)(?=\*)
This gives me the string in between but Im having difficulties in doing the swap.
Use String#replace, you only need to capture the text you want to preserve:
var result = theString.replace(/\bb\*(.*?)\*/g, "<b>$1</b>");
The \b at the begining of the regex means word boundary so that it only matches bs that are not part of a word. $1 means the first captured group (.*?).
Example:
var str1 = "b*any string here* and after that b*string b*.";
var str2 = `b*foo bar
foo bar*
bb*foo bar* (b is not after a whitespace or beginning of string).`;
console.log(str1.replace(/\bb\*(.*?)\*/g, "<b>$1</b>"));
console.log(str2.replace(/\bb\*(.*?)\*/g, "<b>$1</b>"));
You could use \b(?:b\*)(.+?)(?:\*), so
const result = yourString.replace(/\b(?:b\*)(.+?)(?:\*)/, "<b>$1</b>");
See the 'Replace' tab https://regexr.com/447cq
Related
I have a main string like this:
const mainString: string = `
let myStr: string = "Hello world";
function x() { }
let otherVar: number = 4;
let otherString: string = "Other string value";
// something else INSIDE the main string
`;
Now I need to format this main string, but substrings cause unwanted stuff, so I need to get them.
The regex I used until now was: /"([^"]*)"/g.
With it I would get e.g. ['"Hello world"', '"Other string value"'] (in the mainString context from above).
But having a "\"" inside one of these substring, would throw off my regex and give me the part from the beginning until the \" and then, if some other substring was used anywhere else, give me the real end of the substring " (falsly as a start symbol) until the start of the next substring...
One important thing: I have absolutly no control what so ever about anything before and beyont the "substring value".
What would be the correct regex for my usecase?
Try this one
"((\\"|[^"])*?)"
\\" - mean look for \"
| - or
*? - for no greedy
see: regex101
I want to write a regular expression, in JavaScript, for finding the string starting and ending with :.
For example "hello :smile: :sleeping:" from this string I need to find the strings which are starting and ending with the : characters. I tried the expression below, but it didn't work:
^:.*\:$
My guess is that you not only want to find the string, but also replace it. For that you should look at using a capture in the regexp combined with a replacement function.
const emojiPattern = /:(\w+):/g
function replaceEmojiTags(text) {
return text.replace(emojiPattern, function (tag, emotion) {
// The emotion will be the captured word between your tags,
// so either "sleep" or "sleeping" in your example
//
// In this function you would take that emotion and return
// whatever you want based on the input parameter and the
// whole tag would be replaced
//
// As an example, let's say you had a bunch of GIF images
// for the different emotions:
return '<img src="/img/emoji/' + emotion + '.gif" />';
});
}
With that code you could then run your function on any input string and replace the tags to get the HTML for the actual images in them. As in your example:
replaceEmojiTags('hello :smile: :sleeping:')
// 'hello <img src="/img/emoji/smile.gif" /> <img src="/img/emoji/sleeping.gif" />'
EDIT: To support hyphens within the emotion, as in "big-smile", the pattern needs to be changed since it is only looking for word characters. For this there is probably also a restriction such that the hyphen must join two words so that it shouldn't accept "-big-smile" or "big-smile-". For that you need to change the pattern to:
const emojiPattern = /:(\w+(-\w+)*):/g
That pattern is looking for any word that is then followed by zero or more instances of a hyphen followed by a word. It would match any of the following: "smile", "big-smile", "big-smile-bigger".
The ^ and $ are anchors (start and end respectively). These cause your regex to explicitly match an entire string which starts with : has anything between it and ends with :.
If you want to match characters within a string you can remove the anchors.
Your * indicates zero or more so you'll be matching :: as well. It'll be better to change this to + which means one or more. In fact if you're just looking for text you may want to use a range [a-z0-9] with a case insensitive modifier.
If we put it all together we'll have regex like this /:([a-z0-9]+):/gmi
match a string beginning with : with any alphanumeric character one or more times ending in : with the modifiers g globally, m multi-line and i case insensitive for things like :FacePalm:.
Using it in JavaScript we can end up with:
var mytext = 'Hello :smile: and jolly :wave:';
var matches = mytext.match(/:([a-z0-9]+):/gmi);
// matches = [':smile:', ':wave:'];
You'll have an array with each match found.
Sentences:
Hey checkout Hello World <- SHOULD BE INCLUDED
hello world is nice! <- SHOULD BE INCLUDED
Hhello World should not work <- SHOULD NOT BE INCLUDED
This too Hhhello World <- SHOULD NOT BE INCLUDED
var phraseToSearch = "Hello World";
Do note: sentence.ToLower().IndexOf(phraseToSearch.ToLower()) would not work as it would include all the above sentences while the result should only include sentences 1 and 2
You can use regular expression to match a character pattern with a string.
The regular expression is simply looking for Hello World the exact letters you are looking for with \b a word border and using the i case insensitive modifier.
Regex has a method test that will run the regular expression on the given string. It will return a true if the regular expression matched.
const phraseToSearch = /\bhello world\b/i
const str1 = 'Hey checkout Hello World'
const str2 = 'hello world is nice!'
const str3 = 'Hhello World should not work'
const str4 = 'This too Hhhello World'
console.log(
phraseToSearch.test(str1),
phraseToSearch.test(str2),
phraseToSearch.test(str3),
phraseToSearch.test(str4)
)
You probably want to use a regular expression. Here are the things you want to match
Text (with spaces surrounding it)
... Text (with space on one side, and end of text on the other)
Text ... (with space on one side, and start of side on the other)
Text (just the string, on its own)
One way to do it, without a regular expression, is just to put 4 conditions (one for each bullet point above) and join them up with a &&, but that would lead to messy code.
Another option is to split both strings be spaces, and checking if one array was a subarray of another.
However, my solution uses a regular expression - which is a pattern you can test on a string.
Our pattern should
Look for a space/start of string
Check for the string
Look for a space/end of string
\b, according to this, will match spaces, seperators of words, and ends of strings. These things are called word boundries.
Here is the code:
function doesContain(str, query){ // is query in str
return new RegExp("\b" + query + "\b", "i").test(str)
}
The i makes the match case insensitive.
I have a string like the following:
SOME TEXT (BI1) SOME MORE TEXT (BI17) SOME FINAL TEXT (BI1234)
Question
I am trying to make a regex to get just the information between the curly brackets, for example the end string would look like:
BI1 BI17 BI1234
I have found this example on stackoverflow which will get the first value BI1, but will ignore the rest after.
Get text between two rounded brackets
this is the REGEX I created from the above link: /\(([^)]+)\)/g but it includes the brackets, I want to remove these.
I am using this website to attempt to solve this query which has a testing window to see if the regex entered works:
http://www.regexr.com
Additional Information
there can be any amount of numbers also, which is why I have given 3 different examples.
this is a continous string, not on seperate lines
thanks for any help on this matter.
While this isn't possible using just regexes, you can do it with string#split and the following regex:
\).*?\(|^.*?\(|\).*?$
Yielding code that looks a bit like this:
function getBracketed(str) {
return str.split(/\).*?\(|^.*?\(|\).*?$/).filter(Boolean);
}
(You need to filter out the empty strings that'll appear at the beginning and end if you do it this way - hence the extra operation).
Regex demo on Regex101
Code demo on Repl.it
If you need to keep all inside parentheses and remove everything else, you might use
var str = "SOME TEXT (BI1) SOME MORE TEXT (BI17) SOME FINAL TEXT (BI1234)";
var result = str.replace(/.*?\(([^()]*)\)/g, " $1").trim();
console.log(result);
If you need to get only the BI+digits pattern inside parentheses, use
/.*?\((BI\d+)\)/g
Details:
.*? - match any 0+ chars other than linebreak symbols
\( - match a (
(BI\d+) - Group 1 capturing BI + 1 or more digits (\d+) (or [^()]* - zero or more chars other than ( and ))
\) - a closing ).
To get all the values as array (say, for later joining), use
var str = "SOME TEXT (BI1) SOME MORE TEXT (BI17) SOME FINAL TEXT (BI1234)";
var re = /\((BI\d+)\)/g;
var res =str.match(re).map(function(s) {return s.substring(1, s.length-1);})
console.log(res);
console.log(res.join(" "));
I want to match these kind of hashtag pattern #1, #4321, #1000 and not:
#01 (with leading zero)
#1aa (has alphabetical char)
But special character like comma, period, colon after the number is fine, such as #1.. Think of it as hashtag at the end of the sentence or phrase. Basically treat these as whitespace.
Basically just # and a number.
My code below doesn't meet requirement because it takes leading zero and it has an ugly space at the end. Although I can always trim the result but doesn't feel it's the right way to do it
reg = new RegExp(/#[0-9]+ /g);
var result;
while((result = reg.exec("hyha #12 gfdg #01 aa #2e #1. #101")) !== null) {
alert("\"" + result + "\"");
}
http://jsfiddle.net/qhoc/d3TpJ/
That string there should just match #12, #1 and #101
Please help to suggest better RegEx string than I had. Thanks.
You could use a regex like:
#[1-9]\d*\b
Code example:
var re = /#[1-9]\d*\b/g;
var str = "#1 hyha #12 #0123 #5 gfdg #2e ";
var matches = str.match(re); // = ["#1", "#12", "#5"]
This should work
reg = /(#[1-9]\d*)(?: |\z)/g;
Notice the capturing group (...) for the hash and number, and the non capturing (?: ..) to match the number only if it is followed by a white space or end of string. Use this if you dont want to catch strings like #1 in #1.. Otherwise the other answer is better.
Then you have to get the captured group from the match iterating over something like this:
myString = 'hyha #12 gfdg #01 aa #2e #1. #101';
match = reg.exec(myString);
alert(match[1]);
EDIT
Whenever you are working with regexps, you should use some kind of tool. For desktop for instance you can use The regex coach and online you can try this regex101
For instance: http://regex101.com/r/zY0bQ8