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}:')
Related
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 2 years ago.
Improve this question
I'm using React (hooks) and in a component I have postcodes with a space in them in strings:
eg "B72 1JL".
I need to remove the space in the middle of the postcodes so it is "B721JL" and also possibly any spaces before or after the postcode.
I've googled for ages and cannot find anything that will work. I know i probably need regex...but pretty confused!
Help appreciated.
Use String.prototype.replace() to change the space for an empty space.
const str = "B72 1JL";
// Replacing " " (space) to "" empty space
const res = str.replace(/ /g, '')
console.log(res); // BJ721JL
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 2 years ago.
Improve this question
I would to exclude first and last word from the string in JS regex. Please suggest.
Example strings
QueueName default_queue end
QueueName default queue end
I would like to match default_queue and default queue
You could use capture groups
var str = "QueueName default_queue end"
var matchArray = str.match(/^\w+\s+(.+)\s+\w+$/) // capture with paranthesis
console.log(matchArray[1]) // default_queue
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.
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
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