-6

-ANSWER to this example is 'var pattern = /(\\n)|(\\r)|(\\t)/g;'

-ANSWER to question is, you can put each pattern in parentheses, separate with a pipe symbol and assign the global identifier at the end of the regex.

TA DA! :)

TRY THIS LINK FOR A HELPFUL REGEX TOOL: https://regex101.com/#javascript

I thought that simple questions like how do you combine regexes would get a simple answer - i don't believe there's a straight answer on the tutorial. But i recommend reading the manual.

I've read a few suggested answers most seem to focus on the pipe OR operator, but i want to combine regexes. Shamefully newbie at regexes. Anyway, I want to remove special characters from tab, space and return. Here's what I have:

            var pattern1 = /(\\n)/g;
            var pattern2 = /(\\t)/g;
            var pattern3 = /(\\r)/g;

            $heading = $heading.replace(pattern1, "");
            $heading = $heading.replace(pattern2, "");
            $heading = $heading.replace(pattern3, "");

This works, however it is repeating and I can't find any info on how to create one regex pattern that will look for all and remove them.

sjingo
  • 1
  • 5
  • 1
    @sjingo Please clarify whether you want to replace *literal* `\n`s, etc. or *actual* newlines, etc. – Biffen Mar 30 '16 at 10:56
  • OP, please clarify your question. "I want to remove all special characters" that's not what your current regex does. Please [edit] and make it clear what it is that's the problem. – Madara's Ghost Mar 30 '16 at 11:00
  • hi, thank you for all your comments. i enjoy learning for myself but am on a deadline for something hence the post. – sjingo Mar 30 '16 at 11:22
  • not sure why this is marked down, but please rest assured i have been googling for hours and read through lots of StackOverflow answers first. – sjingo Mar 30 '16 at 11:23
  • @Wiktor - that didn't work Wiktor. thanks though. – sjingo Mar 30 '16 at 11:30
  • @Biffen - hi Biffen. so, i want to simply remove any tabs, spaces or returns from my string, which i will pass out as json format from an ajax call to a php file / db and then that will help recreate elements on another page. – sjingo Mar 30 '16 at 11:32
  • @sjingo So `/\n/` rather than `/\\n/`? – Biffen Mar 30 '16 at 11:33
  • @Wiktor and MaraUchiha - i admit it. i need to read up. as you can appreciate it's not a quick process. I really appreciate all the responses . I still don't have a solution but my original code works – sjingo Mar 30 '16 at 11:34
  • @sjingo Then you should try user6134246's answer - that is what you need. – Wiktor Stribiżew Mar 30 '16 at 11:38
  • @Wiktor - i will try that thanks. all the links are useful. shame i didn't quite get to combine my statements. your suggestion did not work. – sjingo Mar 30 '16 at 11:39
  • If your code works, always consider posting the question at [codereview.se] – Wiktor Stribiżew Mar 30 '16 at 11:40
  • i take it none of you actually know the answer then - so really this has been a complete waste of time. – sjingo Mar 30 '16 at 11:51
  • 6
    You've gotten 2 correct answers to your very vague question. You've completely ignored anything we said about editing and clarifying what you want, and you're complaining that this was a waste of *your* time? I'm closing this question, and I apologize @WiktorStribiżew for disputing his decision earlier. – Madara's Ghost Mar 30 '16 at 11:56
  • lol. don't reply if it wastes your time. i thought this place was meant to have helpful people posting helpful answers. i couldn't get your solution working sorry. – sjingo Mar 30 '16 at 13:41

2 Answers2

1

You can combine them using logical ORs.

var pattern = /(\\t|\\n|\\r)/g;
$heading = $heading.replace(pattern1, "");

Using Wiktor Stribiżew approach, but for escaped special chars you could use/\\[trn]/g to match a backslash followed by any of the three characters.

0

Use the bar | to separate patterns with a logical OR. For your example, I would use the pattern

var pattern = /\r|\t|\n/g;

You can also use pattern.source to get the actual regex expression. That way, you can combine them at a later stage. Like this:

var pattern1 = /\r/;
var pattern2 = /\t/;
var combined = new RegExp(pattern1.source + '|' + pattern2.source);
combined.source
=> '\\r|\\t'

That pattern will match any carriage return, tab or newline in your test string. See here for a working example.

Also, check out https://regex101.com/#javascript. It's a great tool for figuring out regexes.

larlon
  • 517
  • 1
  • 5
  • 16