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 have the following user queries:
Boeing AND Airbus OR twitter
Boeing AND Airbus
I need to split words in them by AND, OR so, in the end, I will have something like this:
"Boeing" AND "Airbus"
"Boeing" AND "Airbus" OR "twitter"
How can I do this with split or any other method?
Step by step:
split on space
Iterate through elements, if is NOT "and" or "OR" -> quote
const sentence = "Boeing AND Airbus OR twitter"
const split = sentence.split(" ");
const quoted = split.map(e => ["AND", "OR"].includes(e)?e:'"'+e+'"')
console.log(quoted.join(" "))
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 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 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
If a letter has a space in the back, move it to the frot like
ex: " R" → "R ", R could be any letter. Any solution that works will do.
You can use a regular expression along with
String.prototype.replace() to move the space in front of any letter to the back of the letter:
mystr = " R";
mystr = mystr.replace(/ ([a-zA-Z])/, "$1 ");
console.log(mystr);
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 4 years ago.
Improve this question
I have a string like that header#top.header.header--show-offset and I'm struggling to know how could I split it into something like [ 'header', '#top', '.header', '.header--show-offset' ]
Thank you!
You can use regex:
let str ="header#top.header.header--show-offset";
// Keep the delimiter
let res = str.split(/(?=[#.])/gi);
console.log(res);
I would do this in two steps:
Replace id and class symbols with a comma and then the symbol
Split the resulting string by comma
var selectorString = "header#top.header.header--show-offset";
selectorString = selectorString.replace(/#/g, ",#")
selectorString = selectorString.replace(/\./g, ",.");
var selectorList = selectorString.split(",");
console.log(selectorList);
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 6 years ago.
Improve this question
So I have a .txt file that has the entire dictionary in it. It is separated so that each line is a new word:
EX:
this
is
what
it
looks
like
I need help in putting all of these words into a single array. I am new to Javascript so give me a break if this is a dumb question.
You need to use a regex to "split" your string on the newline (\n), like so:
var dictionary = "this\nis\nwhat\nit\nlooks\nlike"
dictionary.split(/[\n]+/g)
// -> ["this", "is", "what", "it", "looks", "like"]
You can learn more about regex and play with it at http://regexr.com/3d4rr
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