Regex for generic but equal prefix [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I am searching for a regular expression that matches and replaces two words within a row with the same but generic prefix and different but definitive suffixes. As a simple example, /x-resses and x-ors/g should match "actresses and actors" and "ancestresses and ancestors". What do I have to replace x with?

The first x should be a capture group containing a pattern that matches the accepted character sequence, the second should be a back reference to it. For example:
const regex = /([a-z]+)resses and \1ors/;
console.log(regex.test('actresses and actors'));
console.log(regex.test('ancestresses and ancestors'));
console.log(regex.test('ancestresses and actors'));

Related

Replace string pattern but keep part of it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I have a long document with some headings I want to replace in a operation.
The headings have the following structure (there are two nouns, both in with a uppercase first character, separated with a whitespace, also the time is dynamic):
let string = 'Firstname Lastname [00:01:02]';
I want to insert some characters at the front and the end of this string, but want to keep the content.
So the desired output should be something like:
let string = '{Firstname Lastname [00:01:02]}:';
I tried a little bit around with RegEx and can catch the time with the following pattern:
\[[0-9]{2}:[0-9]{2}:[0-9]{2}
I figured it out by using captures in my RegEx.
/(\b[A-Z][a-z]* [A-Z][a-z]*( [A-Z])?\b\s\[[0-9]{2}:[0-9]{2}:[0-9]{2}\])/g
This RegEx captures the pattern of my headings into group one. In a replace operation I can then insert the desired content.
string.replace(regex, '{$1}:')

How to use variables with double quotes into Regular Exception? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to replace the "3" into "5". When it is using static it's working fine but when I use it through variable var allvar= '"3"'; it's not working fine.
Here is the jsfiddle link
new RegExp( /[allvar]+/g ); will construct a regular expression matching all uninterrupted sequences of one or more characters from the set a, l, v, a, r.
To construct a regular expression from a variable, you can do this:
new RegExp(allvar, 'g')
It would also be good to escape characters with special meaning to RegExp, unless you intend for allvar to contain regexp source. Unfortunately, RegExp.escape is still not in the language, so one would use a workaround.
new RegExp(escapeRegExp(allvar), 'g')

How to make a particular Text Bold after a Text [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Suppose I have a sample text.
var sample="id:123 Hello How are you id:456 I am fine".
123 and 456 are ids. There can be multiple ids in a sentence.
So, How to make every id bold in the sentence.
And then after that how to remove "id:" from the sample text.
If you're comfortable with using a bit of regular expressions, this snippet will wrap the IDs in a <strong> element and remove the leading id:.
var sample = "id:123 Hello How are you id:456 I am fine";
var converted = sample.replace(/id:(\d+)/g, '<strong>$1</strong>');
Explanation: The content between the slashes - /id:(\d+)/g is regex that:
id: Finds an instance of id:
(\d+) is followed by one or more numerical characters, and stores that in reference $1
g does a global search, replacing all instances rather than just the first.
You can give every id that you want bold a class and then bold it in a css file.
You can write a function to strip the unwanted "id:", though if you show more code I can give you a more accurate answer.

Get part of the string using regexp [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've a strings, which can have a text like:
'some text user#t12# some text'
'username#John# some text'
'some text usersurname#Malks#'
'userphoto#1.jpg#'
How do I get a text between # and # symbols?
There's a typical structure of the part of the string to search for - type#variable#
type is a JS variable type, it's placed before the first #.
variable is a text that I need to get.
I'm searching for a regexp, that return variable, that is between #...#.
The problem is, I'm not too familiar with regexp, can you help me please?
You need to use capture groups, basically in a regex anything in brackets will be part of the cpature group, in this case you want to capture all the characters between two hashes. The any amount of characters regex is .* so this is what you want to capture between two hashes. Once you execute it you will find the match as second in the array (the first will be the string with the hashes.
var type = "";
var myString = "some text user#t12# some text";
var myRegexp = new RegExp(type+"#(.*)#","g");
var match = myRegexp.exec(myString);
alert(match[1]); // t12
any other matches between hashes will be in match[2].. match[n]

Regex expression for replacing string between two characters (including the characters) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
var x = "abc.cde:abc";
I want to replace everything between and including . and : with a space character.
resulting in abc abc
I can't seem to figure out Regex inclusive matches. Help would be greatly appreciate!
You can use:
var x = "abc.cde:abc";
x = x.replace(/\.[^:]*\:/g, ' '); // => "abc abc"
var x = "abc.cde:abc";
x = x.replace(/\..*:/g, ' '))

Categories