Replacement at the line start JS RegEx [closed] - javascript

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 last month.
Improve this question
I have a multiline string for which I need to do the following modification: If a line starts from one or two digits followed by a period, e.g. 1. or 20., that number+period must be replaced with <li>.
I've tried to do it with regexp but in my attempts if the number+period occurs in other part of the line than the start), it also gets replaced with ` and that is undesirable.
Could anyone help me with right regexp?
let text =`
1.
Some text here
Any text here
Some text here
2.
Some text here
Any text here 24
Some text here
30.
Some text here 42.
Any text here
Some text here`;
let regex = /[0-9]./g;
let result = text.replace(regex, '<li>');
document.write(result);

If you need to replace number with LI and number, use parentheses to create capture group and then reference it with dollar sign:
s.replace( /(\d+\.)/g , "<li>$1" )

Your regex must be let regex = /[0-9]+\./g;
let text =`
1.
Some text here
Any text here
Some text here
2.
Some text here
Any text here 2400.
Some text here00.
300.
Some text here
Any text here
Some text here`;
let regex = /[0-9]+\./g;
let result = text.replace(regex, '<li>');
document.write(result);

Related

replace something in a string with something else in JS [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 4 months ago.
Improve this question
First part:
I want to replace the spaces in a string to \n but the first space I want it to be replaced with a double like this: \n\n
var string = "this is something random “
Result needed:
var string = "this\n\nis\nsomething\nrandom\n"
Second part:
I want to replace the string quotes with backticks.
var string = "this\n\nis\nsomething\nrandom\n"
Result needed:
var string = `this\n\nis\nsomething\nrandom\n`
First Part
For what you want, you can just call a replace function to replace the first occurrence, then a replaceAll/regex to replace all others:
let input = "Hello World !"
function replaceFirstThenAll(string, lookfor, replace) {
return string
// replace the first occurance
.replace(lookfor, replace.repeat(2))
// now all others
.replace(new RegExp(lookfor, 'g') /* global regex */, replace)
}
console.log(replaceFirstThenAll(
input,
" ",
"\n"
)) // Hello\n\nWorld\n !
Second Part
The way that you declare a string doesn't matter in js, so there's no way to replace it. No matter which method you use, the string is the same.
"String1" == 'String1' // true
'String2' == `String2` // true

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}:')

Restore a string that has randomly inserted characters [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 1 year ago.
Improve this question
I would like to clean this string How52363262362366are9you?, or any similar string that the user inputs, using wherever character he wants.
This was my first approach :
function buildstring(str){
var reg =/[0-9]/g;
let newStr = str.replace(reg, '');
return newStr;
}
console.log(buildstring('How52363262362366are9you?'));
This way I'm only deleting the digits. I can make the regex even better by adding some non alphanumeric characters, but let's keep it simple.
This function returns Howareyou?. Now I want to separate the words with a space to reconstruct the sentence.
My first idea was using split(''), but of course didn't work...
How to solve this? Is there any way to make that a word starts at point a and ends in point c?
Just change your regex a bit so that it matches grouped numerical characters, and replace each group with a space.
function buildstring(str){
var reg =/[0-9]+/g;
let newStr = str.replace(reg, ' ')
return newStr
}
buildstring('How52363262362366are9you?')
there are several approaches.
You could consider it a case for Functional Programming (since there's a flow of transformations).
Something like this:
function buildstring(str){
return str
.split('')
.filter((character) => !'0123456789'.includes(character))
.join('');
}
console.log(buildstring('How52363262362366are9you?'));
If you use split(' ') (or even break on \s to consider tabs and newlines), you'll have „words” (with interpunction).
By breaking up the code into smaller functions, you can compose them.
For example, the .filter() could be a stripnumerals function.
This way, you can compose the transformations as you please.

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.

How to show first 2 characters and replace all the last by * [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'm wondering how to show the first two characters and replace all last character of a string by symbol *.
Ex: 121,121,121 -> 12x,xxx,xxx .
Thanks
I love using regex when it comes to replace string according to some pattern.
var p = '121,121,121';
var regex = /(?<=.{2})([0-9])/gm;
console.log(p.replace(regex, 'x'));
You can use substring and regular expression. See the sample below.
var str = "121,121,121";
var res = str.substring(0, 2) + '' + str.substring(2, str.length).replace(/[0-9]/g,"x");
alert(res);
Just use substring and replace with a simple regex (to single out digits and keep commas and other punctuation):
const str = "121,121,121";
const obfuscated = `${str.substring(0, 2)}${str.substring(2).replace(/\d/g, "*")}`;
console.log(obfuscated);

Categories