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

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.

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

Is there way to change a substring 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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
I have a string like:
def definition():
I want to change word def (for example), every instance of word def but not the "def"s that are part of other words
like this
console.log("def definition():".specialReplace("def", "abc"));
and result should be
abc definition():
not
abc abcinition():
Use String#replace or String#replaceAll with a regular expression:
const specialReplace = (str) => str.replaceAll(/\bdef\b/g, 'abc')
console.log(specialReplace("def definition")) // abc definition
console.log(specialReplace("def definition def")) // abc definition abc
In the regular expression, \b is a boundary type assertion that matches any word boundary, such as between a letter and a space.
Note that the same sequence \b is also used inside character class regular expression positions ([\b]), to match the backspace character.

How to retrieve substring which is outside of brackets in JS? [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
I want the text which is outside of brackets, for eg.
Text is - Outside (inside)
and what I expect is - Outside
Can someone please help me to achieve this.
You can use slice & use indexOf to get the first (. This will extract all the characters before first (
let str = 'Outside (inside)'
let substr = str.slice(0, str.indexOf('('));
console.log(substr.trim())
If you wanted to remove all bracketed text from the string you could use
let str = 'Outside (inside)test(d 342 dd3d)dd(t423t t)dd()fasf(fsdfds32dfs)';
console.log(str.replace(/(\([\w\d ]*\))+/g, ''))

Replace the Word Before the search term [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 7 years ago.
Improve this question
I am trying to be able to use replace(searchvalue, newvalue); and a user input that would be the search term.
An example would be replace(input,"example text");
But what I want is to be able to have the search term, but instead of replacing the search term, replace the space in front of it.
Ex. is the sentence: "Hi, I am using js to create this!"
and user inputs "js" replace(input, "html and ");
but instead of replacing "js", replace the space in front. So the output sentence would be:
"Hi, I am using html and js to create this!"
Would there be anyway to do this with replace?
You can use a function to handle the replacement in string.replace.
var newString = 'Hi, I am using js to create this!'.replace('js', function(match) {
return 'html and ' + match;
});
console.log(newString); // ... using html and js ...
In the above example I prepend "html and " to the variable match (which has the value of "js"). There is a lot of flexibility when you use a regex instead of a string to find the match.
MDN

How to split string with several char [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 8 years ago.
Improve this question
How to split string with several char
i have to split a scope when there are spaces, comma, dash, etc..( ponctuation) and when words are concatenated ( without a space between variable)
For exemple
testOne="{{test.test}} {{test.test}}{{test.test}}";
The expected output is
"test.test test.testtest.test"
(There are two comma between the first and the second text.text)
You can use Regular Expression to get the result what you wanted.
testOne = "{{test.test}} {{test.test}}{{test.test}}";
console.log(testOne.match(/{{.*?}}/g).map(function(item) {
return item.replace(/[{}]/g, "");
}));
# [ 'test.test', 'test.test', 'test.test' ]
You can achieve your desired output with a simple replace
testOne.replace(/{{(.*?)}}/g, '$1');
// "test.test test.testtest.test"
Remember to set this back to testOne if you want it kept as that variable

Categories