首页 \ 问答 \ 如何使用javascript替换变量模式(how to use javascript replace with variable pattern)

如何使用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
更新时间:2022-01-12 10:01

最满意答案

为了彻底理解它,我们需要了解在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 before sigprocmask() 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 ...
  • 在* nix上,信号是从内核到用户空间的回调。 它们有点类似于中断,可以将其视为从硬件到操作系统的回调。 它们在基于事件的系统中比在Qt中的信号/时隙更相似。 在Qt中,组件可以发出信号,该信号可以由其他组件中的多个插槽接收,并且程序设置信号和插槽之间的连接。在其他系统(Xlib,Win32)中,程序从系统接收预定事件。 * nix支持阻塞和非阻塞套接字。 poll()和select()系统调用允许程序等待I / O事件。 作为poll() / select()的替代方法,当发生I / O事件时,也可以使B ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)