如何使用javascript替换变量模式(how to use javascript replace with variable pattern)
我有一个算术表达式,可以在美国数字中有一个数字(如1000100可以写成一百万一千。我需要在表达式中识别这些模式并将它们转换为各自的数字等价物。
我正在使用下面的代码。
var patternToExtractOperand = '\+|-|\*|\/|%\sof|%|\(|\)'; expr = expr.replace(/patternToExtractOperand(.+?)patternToExtractOperand/g,word2number($1));
word2number是将任意数字的字表示转换为(10 ^ 68)到其各自的数字等价物并且patternToExtractOperand是使用支持的模式准备的列表的函数。
写上面这段代码的正确方法应该是什么? 或者如何使用javascript替换变量模式?
i have an arithmetic expression which can have a number in American number word ( like 1000100 can be written as one million one thousand. I need to identify such patterns in a expression and convert them to their respective numeric equivalent.
I am using below code.
var patternToExtractOperand = '\+|-|\*|\/|%\sof|%|\(|\)'; expr = expr.replace(/patternToExtractOperand(.+?)patternToExtractOperand/g,word2number($1));
word2number is a function which convert word representation of any number till (10^68) into its respective number equivalent and patternToExtractOperand is the list prepared using supported patterns.
What should be the correct way to write above piece of code? or how to use javascript replace with variable pattern?
原文:https://stackoverflow.com/questions/19130264
最满意答案
为了彻底理解它,我们需要了解在linux中如何生成和传递信号。
当内核接收到任何信号请求时,它为该进程设置信号未决标志,提供的信号未被阻止。 现在在返回到用户模式之前,内核检查是否存在用于进程的非阻塞未决信号。 如果是的话,那么kernel在返回用户模式之前更愿意提供该信号。 现在回到你的问题:
如果有一个已经解锁的未决信号(比如SIGUSR1)
我假设信号(SIGUSR1)首先被阻止,然后解除阻塞。 在这种情况下,当用户尝试解除阻塞信号时,信号将在用户模式下恢复用户进程之前传输。 例如。 如果使用
sigprocmask()
来取消阻塞信号,则信号将在sigprocmask()
返回之前传递。将信号SIGUSR1通过调用sigprocmask()传递给进程,该进程在保持SIGUSR1解除阻塞的情况下解除阻塞不同信号?
如果多个未决信号被解除阻塞并准备发送,那么内核会选择具有最低信号数量的信号(当然,同步信号具有比异步信号更高的优先级)以首先发送。
To understand it thoroughly we need to understand how signal are generated and delivered in linux.
When kernel receive any signal request it sets signal pending flag for the process, provided signal is not blocked. Now before returning to user mode kernel checks whether there are nonblocked pending signals are present for process or not. If yes, then kernel prefers to deliver that signal before returning to user mode. Now coming to your question:
if there is a pending signal (say SIGUSR1) that is already unblocked,
I am assuming that signal(SIGUSR1) is first blocked and later unblocked. In this case, when user made an attempt to unblock signal then signal will be delivered before user process resumes in user mode. For ex. if
sigprocmask()
is used to unblock signal then signal will be delivered even beforesigprocmask()
returns.will the signal SIGUSR1 be delivered to the process with a call to sigprocmask() that unblocks a different signal while keeping SIGUSR1 unblocked?
If multiple pending signals are unblocked and ready to deliver then kernel picks the signal which has lowest signal number( of course, synchronous signals have higher priorities over asynchronous signal) to deliver first.
相关问答
更多-
sigaction的基本行为是调用一个简单的回调,如: void (*sa_handler)(int); 。 所以如果你想使用带有3个参数的sigaction句柄void (*sa_sigaction)(int, siginfo_t *, void *); ,您必须使用标志SA_SIGINFO设置struct sigaction的sa_flags字段。 看看手册页: http : //www.kernel.org/doc/man-pages/online/pages/man2/sigaction.2.htm ...
-
我们声明了一个类型为sigset_t的新掩码,然后sigempty(&newmask)意味着初始化newmask,以便排除newmask指向的所有信号,最后将SIGQUIT添加到newmask集。 不完全是。 sigemptyset()排除来自newmask 所有信号(即它是一个空信号集)。 但我不确定以下是什么意思。 是不是将新掩码和sigprocmask()指向的信号SIG_BLOCK到旧掩码? sigprocmask(SIG_BLOCK, &newmask, &oldmask)使用newmask设置新 ...
-
SIGUSR1信号不会发送到您认为的位置。 在多线程程序中, raise函数向当前线程发送信号,在这种情况下是thread_job线程。 所以主线程永远不会看到信号。 您需要保存主线程的线程ID,然后使用pthread_kill向该线程发送信号。 添加新的全局: pthread_t main_tid; 然后在启动新线程之前在init函数中填充它: void init() { main_tid = pthread_self(); ... 然后在message_rcvd ,使用pthread ...
-
好吧,我觉得评论很清楚...... 每当你调用fork()并希望以任何方式与孩子互动时,都需要考虑竞争条件。 如果孩子在父母之前跑了一段时间怎么办? 或相反亦然? 在这种情况下,无法知道在调用fork之后父节点将花多长时间来调用sigsuspend 。 那么如果分叉的孩子完成其sleep并在父母调用sigsuspend之前调用exit sigsuspend ? 然后父母将收到一个它忽略的SIGCHLD ......然后它会调用sigsuspend ,因为SIGCHLD已经交付,所以它将永远不会返回。 唯一1 ...
-
有4个问题: 问题1 设置信号处理程序的代码是错误的。 这2行的顺序是错误的: sigaction(SIGUSR1, &action, NULL); action.sa_handler = handler_sigusr1; 他们应该是 action.sa_handler = handler_sigusr1; sigaction(SIGUSR1, &action, NULL); 问题2 您的父进程在发送SIGUSR1后立即向您的孩子发送SIGKILL。 SIGKILL将终止 ...
-
为了彻底理解它,我们需要了解在linux中如何生成和传递信号。 当内核接收到任何信号请求时,它为该进程设置信号未决标志,提供的信号未被阻止。 现在在返回到用户模式之前,内核检查是否存在用于进程的非阻塞未决信号。 如果是的话,那么kernel在返回用户模式之前更愿意提供该信号。 现在回到你的问题: 如果有一个已经解锁的未决信号(比如SIGUSR1) 我假设信号(SIGUSR1)首先被阻止,然后解除阻塞。 在这种情况下,当用户尝试解除阻塞信号时,信号将在用户模式下恢复用户进程之前传输。 例如。 如果使用sigp ...
-
从信号手册页 : 信号处理是一个每进程属性:在多线程应用程序中,特定信号的配置对于所有线程都是相同的。 所以,是的,当你设置一个信号处理程序时,它将处理发送给进程的信号; 信号不会单独发送到每个线程,它会被发送到任何一个未阻塞正在发送的特定消息的线程。 From the man page for signals: The signal disposition is a per-process attribute: in a multithreaded application, the disposition ...
-
你确定sem_wait()导致信号被阻止吗? 我不认为是这种情况。 sem_wait()的手册页说如果它被信号中断,则从sem_wait()返回EINTR错误代码。 您应该能够处理此错误代码,然后您的信号将被接收。 您是否遇到未收到信号的情况? 我会确保你处理sem_wait()可以返回的错误代码。 虽然可能很少见,但如果你想100%确定你想要100%的基数。 Are you sure sem_wait() causes signals to be blocked? I don't think this i ...
-
显然r10应该保持值8而不是32。 我在内核代码中遇到了4个不同的sigset_t定义; 并且对于它们中的每一个,sizeof()函数返回不同的结果(我记得32,128,4和8)。 只有最后一个与系统调用相关。 内核首先检查$r10 == sizeof(sigset_t) ; 如果不成立,则返回EINVAL (-22)。 对于32位和64位版本, sizeof(sigset_t)值等于8。 Apparently r10 should hold the value 8 instead of 32. I enc ...
-
signal()有什么作用?(What does signal() do?)[2024-05-07]
在* nix上,信号是从内核到用户空间的回调。 它们有点类似于中断,可以将其视为从硬件到操作系统的回调。 它们在基于事件的系统中比在Qt中的信号/时隙更相似。 在Qt中,组件可以发出信号,该信号可以由其他组件中的多个插槽接收,并且程序设置信号和插槽之间的连接。在其他系统(Xlib,Win32)中,程序从系统接收预定事件。 * nix支持阻塞和非阻塞套接字。 poll()和select()系统调用允许程序等待I / O事件。 作为poll() / select()的替代方法,当发生I / O事件时,也可以使B ...