Javascript - replace pattern but keep alphabets [duplicate] - javascript

This question already has answers here:
How can I replace a regex substring match in Javascript?
(4 answers)
How to replace captured groups only?
(7 answers)
Closed 3 years ago.
I was wondering, what is the best way to implement the following case:
vm.elasticQuery = userQuery.replace(/~='[a-zA-Z]+'/g,':*[a-zA-Z]+*')
In other words, I would like to replace every pattern that looks like this ~='SomeLetters' to a pattern that looks like this :*SomeLetters*. Take into account that I do not want to remove/change the alphabets but only the outside.
Your help is appriciated.

Related

Split by word not contained in parentheses [duplicate]

This question already has answers here:
How to split by commas that are not within parentheses?
(6 answers)
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 3 years ago.
Here is the my string
var string = 'Title:(India OR America) OR Keyword:(Education)';
After splitting with "OR" it is showing
["Title:(India", "America)", "Keyword:(Education)"]
But what I need is the below one.
["Title:(India OR America)", "Keyword:(Education)"]
Could anyone please help on how to split the string to get the required result.
To achieve this you'll need to use a negative lookahead to only find the OR string where it's not contained in parentheses. Try this:
var input = 'Title:(India OR America) OR Keyword:(Education)';
var output = input.split(/(?!\(.*)\s?OR\s?(?![^(]*?\))/g);
console.log(output);

Regex to get string between two specific tags [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I have a string like:
[device]Some device[/device]
I am trying to write regex that will return only "Some device".
/[device](.*?)[/device]/i.exec(str) // doesn't work
/[device](.*)[\/device]/g.exec(text) // doesn't work
I am not sure how to extract it?
I already tried solution from Regular Expression to get a string between two strings in Javascript and it doesn't work for string like [][/]
[device]Some device[/device]
return
evice]Some device[/device] and all other strings.

Javascript string split with regex multiple conditions [duplicate]

This question already has answers here:
Javascript: Split a string by comma, except inside parentheses
(7 answers)
Regex split string by spaces taking into account nested brackets
(2 answers)
Closed 5 years ago.
I'm trying to split string
x#x,a,b,c,d(some(other,arg2),p)#yyy into array
['x#x','a','b','c','d(some(other,arg2),p)#yyy'].
The problem i faced is that i cannot split just comma /\,/g since members that contains already commas would split also. Any ideas?

What are the ways to search for several alternate characters without creating a capturing group [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
Suppose I have a task to search for words that start with either a or b, for example all or bll words. I know I can use grouping and or operator like this:
(a|b)ll
But this creates a capturing group. Are there any other options?
How about using square brackets instead of parens like: [ab]ll ?

What is wrong with this RegExp? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I have this string
§serverUrl§/image/?url=§externalMedia.data.image§
and I want to match only the string inside the § character.
(serverUrl and externalMedia.data.image).
I'm using this pattern :
/§.+§/g
but it catches also the characheters between the two groups.
Any help would be appreciate. Thanks
You will have to make it lazy using ? like this §.+?§
Regex101 Demo

Categories

Resources